开发者

i am trying to write a simple textbox (for people to type url in it) with a button in html

i am trying to write a simple textbox (for people to type url in it) with a button in html. when the button is clicked, it will send the url of the current website that I am browsing to the url that is listed in the textbox using the POST method. is it possible? i have been looking on forums but don't really know which is the right one cos it seems that there are various way of doing it and i don't really know how to edit them.

my current code:

<html>
<head>
<title>YouTube</title>

<script type="javascript/text">

function handleButtonEnterClick(tab) {
//TODO:
    var textbox_url = document.getElementById("url_textbox");
    var textbox_value = textbox_url.value; //eg. value = "www.google.com"

       //Need to have a POST method written here to send the url of the current 
       //webpage for example www.youtube.com to url listed in the textbox, 
       //for example www.google.com
       //May I know how can I d开发者_运维问答o it? Thanks.
}
</script>
</head>

<body>

<div id ="container">
<p>Enter URL:</p>
<input type="text" id="url_textbox" name="url_textbox" />
<input type="button" id="button_enter" name="button_enter" 
       value="enter" onclick="handleButtonEnter" />
</div>

</body>
</html>


What you need is a <form> element, which a) has action attribute to indicate where to send the data; b) on submit sends the data (I've added an extra <input type='hidden'> to store your current pages url for sending).

<script type="javascript/text">    
    function handleButtonEnterClick() {
        var textbox_value = document.getElementById("url_textbox").value;
        document.getElementById('myUrl').value = window.location;
        var form = document.getElementById('myForm');
        form.action = textbox_value;
        form.submit();
    }
</script>

<div id="container">
    <form action="" method="post" id="myForm">
        <p>Enter URL:</p>
        <input type="hidden" id="myUrl" name="url" />
        <input type="text" id="url_textbox" name="url_textbox" />
        <input type="button" id="button_enter" name="button_enter" 
               value="enter" onclick="handleButtonEnter" />
    </form>
</div>


This should work:

<html>
  <head>
    <title>YouTube</title>

    <script type="javascript/text">

      function handleButtonEnterClick(tab) 
      {
        var textbox_url = document.getElementById("url_textbox");
        var textbox_value = textbox_url.value; //eg. value = "www.google.com"

        //Set the form action to the textbox value
        var the_form = document.getElementById("the_form");
        the_form.setAttribute("action", textbox_value);

        //Set the value of the url field to the current url
        document.getElementById("url").setAttribute("value", window.location);

        the_form.submit();
      }
    </script>
  </head>

  <body>

    <div id ="container">
      <form action="" method="post" id="the_form">
        <p>Enter URL:</p>
        <input type="hidden" name="url" id="url" />
        <input type="text" id="url_textbox" name="url_textbox" />
        <input type="button" id="button_enter" name="button_enter" 
          value="enter" onclick="handleButtonEnter" />
      </form>
    </div>

  </body>

</html>


The current page location is stored in the JavaScript variable "window.location.href" (thats in Chrome, might be different elsewhere).

You also need to set the action of your form to the URL in the textarea. Suggest you put an id tag on the html form element, and use that id tag to set the action property of the form to the contents of the textbox as part part of the buttons onclick handler.


There are two options:

  • Use a HTML form, as Ant has shown, set the action and method attributes. Add a submit button inside the form (along with your textbox). When you click on the Submit button, your data will get posted.
  • Use AJAX to post your form if you want to stay in the current page
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜