开发者

HTML - Put SELECT tag content into INPUT type = "text"

I have a form in a webpage where I would like to put the selected item in a drop down list into a testbox. The code I have till now is the following:

            <form action = "">
                <select name = "Cities">
                  <option value="----">--Select--</option>
                  <option value="roma">Roma</option>
                  <option value="torino">Torino</option>
                  <option value="milan">Milan</option>
                </select>
                <br/>
                <br/>
                <input typ开发者_运维百科e="button" value="Test">
                <input type="text" name="SelectedCity" value="" />
            </form>

I think I need to use javascript .... but any help? :-)

thanks


You can add JavaScript directly into the button:

<input type="button" onclick="
    var s = this.form.elements['Cities'];
    this.form.elements['SelectedCity'].value =
      s.options[s.selectedIndex].textContent">


 <script type="text/javascript">
    function OnDropDownChange(dropDown) {
        var selectedValue = dropDown.options[dropDown.selectedIndex].value;
        document.getElementById("txtSelectedCity").value = selectedValue;
    }
 </script>

        <form action = "">
            <select name = "Cities" onChange="OnDropDownChange(this);">
              <option value="----">--Select--</option>
              <option value="roma">Roma</option>
              <option value="torino">Torino</option>
              <option value="milan">Milan</option>
            </select>
            <br/>
            <br/>
            <input type="button" value="Test">
            <input type="text" id="txtSelectedCity" name="SelectedCity" value="" />
        </form>


In fact you don't need any JS to do that, simply HTML can do it for you as follows:

<form action="a.php" method="post">
  <select name = "Car">
    <option value="BMW">BMW</option>
    <option value="AUDI">AUDI</option>
  </select>
  <input type="submit" value="Submit">
</form>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜