JavaScript要怎么判断select标签选中,当查询完后还是显示选中条件

来源:百度知道 编辑:UC知道 时间:2024/07/02 05:29:26
点击查询后,还得显示查询条件,具体要怎么操作?

首先可以响应select的onchange事件来调用JS编写的事件响应函数,如<select id="select1" name="select1" onchange="outputSelect();"><option>...</select>

然后编写事件响应函数:
如果select位于表单(form1)中,select的name为select1,则可使用如下方法:
//获得用户选中的项的索引
var index=window.document.form1.select1.selectedIndex;
//根据索引获得该选项的value值
var val=window.document.form1.select1.options[index].value;

如果select并非表单元素,假设select的id为select1,则如下:
var index=window.document.getElementById("select1").selectedIndex;
var val=window.document.getElementById("select1").options[index].value;

如果要输出选择结果,假设HTML中定义了一个<div id="output"></div>,则如下输出:
window.document.getElementById("output").innerText=val;

给出一个我写的示例:
function outputSelect()
{
//获取用户选中的项的索引
var index=window.document.getElementById("select1").selectedIndex;
//根据index获取选中项的value值
var