我需要在提交下拉列表中获取所选文本和selectedIndex。以下代码返回第一个索引始终为0,第一个选项为text..for例如,如果我选择了三个,则显示0索引和一个文本..
function myfunc()
{
`enter code here`var x = document.getElementById("listt").selectedIndex;
var y = document.getElementById("listt").options;
alert("Index: " + y[x].index + " is " + y[x].text);
}
<select id="listt" >
<option >one</option>
<option >two</option>
<option>three</option>
<option >four</option>
</select>
<input type="Submit" value="submit " name="Submit" onclick="myfunc()"></input>
答案 0 :(得分:0)
试试这段代码。
<select id="listt" >
<option >one</option>
<option >two</option>
<option>three</option>
<option >four</option>
</select>
<input id="btnSubmit" type="Submit" value="submit " name="Submit" />
function myfunc() {
var x = document.getElementById("listt").selectedIndex;
var y = document.getElementById("listt").options;
alert("Index: " + y[x].index + " is " + y[x].text);
}
document.getElementById ("btnSubmit").addEventListener ("click", myfunc, false);