如何从HTML选择选项列表中获取值

时间:2015-08-11 07:06:39

标签: forms form-submit onsubmit

<form action="" method="post"> <select name="select1"> <option value="value1">Value one</option> <option value="value2">Value two</option> </select> <input type="submit" name="submit" value="Go"/> </form>

Onsubmit,我需要在提交时打印Value one。所有帖子都使用$_POST['select1']来获取值。但它只会给value1

1 个答案:

答案 0 :(得分:1)

试试这个

&#13;
&#13;
function changeValue(){

var e = document.getElementById("select1");
var strUser = e.options[e.selectedIndex].text;
document.getElementById("submit").value = strUser;
}  
&#13;
<form action="" method="post">
  <select name="select1" id="select1">
    <option value="value1">Value one</option>
    <option value="value2">Value two</option>
  </select>
  <input type="submit" name="submit" id="submit" value="Go" onclick="changeValue();"/>
</form>
&#13;
&#13;
&#13;