我有一个如下所示的下拉列表。
HTML和PHP
<select id="selectform_0">
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
</select>
<input type="button" onclick="selectValue(<?php echo $id; ?>)
">
和Javascript
<script>
function selectValue(id)
{
//take select value
alert(selectvalue);
}
</script>
无论如何都是通过javascript 严格从不使用<form>
答案 0 :(得分:1)
是的,你可以。
<强>标记强>
<select id='myselectform' name="selectform_0">
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
</select>
<强> JS 强>
//If you want the values when an action takes place, say, onchange
document.getElementById("myselectform").onchange = function(){
alert(this.value)
}
//Or this way,
var t = document.getElementById("myselectform");
var selectedElem = t.options[t.selectedIndex].value;
//Or if you just want the whole list of values then do something like this,
var t = document.getElementById("myselectform").getElementsByTagName("option");
for(var i=0,len=t.length; i<len; i++)
{
alert(t[i].getAttribute("value"))
}
答案 1 :(得分:1)
试试这个
的Javascript
<script>
function selectValue(id)
{
var obj = document.getElementById("selectform_"+id).options[document.getElementById("selectform_"+id).selectedIndex].value;
alert(obj);
}
</script>
HTML
<select id="selectform_0">
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
</select>
<input type="button" onclick="selectValue(<?php echo $id; ?>) ">
答案 2 :(得分:-1)
你可以使用jQuery
$("#myselectform").val();
或
$("#myselectform").attr("selected");