HTML - Put SELECT tag content into INPUT type = “text”

半城伤御伤魂 提交于 2019-12-09 17:41:01

问题


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 type="button" value="Test">
                <input type="text" name="SelectedCity" value="" />
            </form>

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

thanks


回答1:


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">



回答2:


 <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>



回答3:


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>


来源:https://stackoverflow.com/questions/2539520/html-put-select-tag-content-into-input-type-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!