最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I want to insert input values to link - Stack Overflow

programmeradmin2浏览0评论

I have to link three input values to the link. How I can do in easy way and I click the link I want open the URL.

<html>
    <script type="text/javascript">
        function changeText2(){
        var userInput0 = document.getElementById('userInput').value;
        var userInput11 = document.getElementById('userInput').value;
        var userInput21 = document.getElementById('userInput2').value;
        document.write("userinput");
    }
    </script>

    Here is a link : <a href="/00ON0000000FOHS?pc0=userInput0&pn0=userInput11&pv0=userInput21" >nothing here yet</a> <br/>
    <input type='text' id='userInput' value='Enter Search String Here' />
    <input type='text' id='userInput1' value='Enter Text Here' />
    <input type='text' id='userInput2' value='Enter Text Here' />
    <input type='button' onclick='changeText2()' value='Change Link'/>
</html>

I have to link three input values to the link. How I can do in easy way and I click the link I want open the URL.

<html>
    <script type="text/javascript">
        function changeText2(){
        var userInput0 = document.getElementById('userInput').value;
        var userInput11 = document.getElementById('userInput').value;
        var userInput21 = document.getElementById('userInput2').value;
        document.write("userinput");
    }
    </script>

    Here is a link : <a href="/00ON0000000FOHS?pc0=userInput0&pn0=userInput11&pv0=userInput21" >nothing here yet</a> <br/>
    <input type='text' id='userInput' value='Enter Search String Here' />
    <input type='text' id='userInput1' value='Enter Text Here' />
    <input type='text' id='userInput2' value='Enter Text Here' />
    <input type='button' onclick='changeText2()' value='Change Link'/>
</html>
Share Improve this question edited Dec 16, 2013 at 10:40 Mr. Polywhirl 48.9k12 gold badges93 silver badges144 bronze badges asked Dec 16, 2013 at 10:33 tejdeeptejdeep 1151 silver badge11 bronze badges 5
  • 1 your question is not clear.. – Kamlesh Meghwal Commented Dec 16, 2013 at 10:34
  • Do you want to make new link? or just need to create parameters for query string from the inputs? It seems that you want to add parameters ! – Dhaval Marthak Commented Dec 16, 2013 at 10:36
  • Use form tag and set this href value to action. and method as Get, and in the hyper link set href as # and on click of hyperlink call form. submit() – Priyank Commented Dec 16, 2013 at 10:37
  • i am getting three inputs from the user and that three data to be inserted in the url ""/00ON0000000FOHS?pc0=**userInput0&pn0=**userInput11**&pv0=**userInput21"" and clicking the button .the url has to opened. – tejdeep Commented Dec 16, 2013 at 10:39
  • Just use a form and a submit button. – Quentin Commented Dec 16, 2013 at 10:41
Add a ment  | 

4 Answers 4

Reset to default 3

If Your link is static than you can create your href link in script it self like :

var link = document.getElementById("test");
link.href = "/00ON0000000FOHS?pc0="+userInput0+"pn0="+userInput11+"pv0="+userInput21;  

Here test is id of your <a> element.
You can crate href link by appending parameter value.

I wrote a jsFiddle with a working solution:

http://jsfiddle/t8kAS/

Note that I removed the Button and put the event on the onchange event.

$(".jsCustomField").on("change", function() {
    var customLink = linkTemplate.replace("{0}", $input1.val())
                                    .replace("{1}", $input2.val())
                                    .replace("{2}", $input3.val());
    $dynLink.attr("href", customLink);
});

Hope this helps!

The most standard-based way is using form with GET method

<form method='GET' action='/00ON0000000FOHS?'>
    <input type='text' id='userInput' name='pc0' value='Enter Search String Here' />
    <input type='text' id='userInput1' name=pn0' value='Enter Text Here' />
    <input type='text' id='userInput2' name='pv0' value='Enter Text Here' />
    <input type='submit' value='Change Link'/>
</form>

The name part of the input tag will be used as the argument key, and the value entered into the input field is the value in the get request.

Change your function to:

function changeText2(){
      var userInput0 = document.getElementById('userInput').value;
      var userInput11 = document.getElementById('userInput1').value;
      var userInput21 = document.getElementById('userInput2').value;
      //document.write("userinput");
      window.open("/00ON0000000FOHS?pc0="+userInput0+"&pn0="+userInput11+"&pv0="+userInput21, "_self");
}

If you want it to open in a new window, use "_blank" in the 2nd argument of window.open.

After reading STO's answer (it's inaccurate) I realize, you actually don't need to use js for this. The standard html form was built for exactly this. Use just this, no js required:

<form method='GET' action='/00ON0000000FOHS'>
    <input type='text' id='userInput' name='pc0' value='Enter Search String Here' />
    <input type='text' id='userInput1' name='pn0' value='Enter Text Here' />
    <input type='text' id='userInput2' name='pv0' value='Enter Text Here' />
    <input type='submit' value='Change Link'/>
</form>

The name part of the input tag will be used as the argument key, and the value entered into the input field is the value in the get request.

发布评论

评论列表(0)

  1. 暂无评论