hot以获取所选文本,并在select中选择索引
hot to get the selected text and selectedIndex in select?
我需要在提交时在下拉列表中获取所选文本和selectedIndex。。下面的代码返回第一个索引,第一个选项文本始终为0。。例如,如果我选择了三个,它显示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>
试试这个代码。
<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);
相关文章:
- 使用clickToggle并在单击另一个元素时关闭元素
- 使用javascript检查多个输入值,并在1次检查中标记多个输入框
- 使用jQuery动态添加表并在其中动态添加行
- 遍历类元素数组,并在jquery中选择同级元素
- 如何缩短MongoDB ObjectId并在Mongoose模式中使用它
- 如何覆盖锚点元素's href目标,并在我点击转到目标javascript时删除其他错误
- 通过ajax将坐标传递到php服务器端,并在处理后检索到javascript
- ng消息仅在触摸时显示错误,并在错误的初始显示上转换
- Javascript对象类在单击时打开窗口进行颜色选择,并在更改时替换对象背景颜色
- 从 select 中获取 ID,并在 if 语句中使用它们
- hot以获取所选文本,并在select中选择索引
- 将HTML Select附加到输入,并在用户单击“保存”按钮后存储它们的值.我正在处理的
- 向Select动态添加元素,并在JQuery中设置selectindex
- 如何在Bootstrap select中的下拉菜单关闭时触发事件,并在Backbone.js中进行多选
- 定义函数并在select语句中用作onchange,而不是直接使用.change(function)
- 如何使用Ajax从select中获取值并在PHP中使用
- Jquery创建了select,并在更改时触发
- 从输入字段获取变量值,并在SELECT-WHERE语句中使用它
- 获取一个select's值,并在angularJs中更改另一个select后将其更新为一个变量
- Highcharts: set 'select'在一个点上状态并在鼠标悬停后保持它