法2.从json读取
<input class="easyui-combobox" name="" id="" data-options="valueField:'id',textField:'name',url:'json文件或AJAX事件'" />
方法:获取多选下拉框对象数组→循环判断option选项的selected属性(true为选中,false为未选中)→使用value属性取出选中项的值。实例演示如下:
1、HTML结构
<select id="test" multiple="true"><option value="option-A">option-A</option>
<option value="option-B">option-B</option>
<option value="option-C">option-C</option>
<option value="option-D">option-D</option>
</select>
<input type="button" value="确定" onclick="fun()" />
2、javascript代码
function fun(){var select = document.getElementById("test")
var str = []
for(i=0i<select.lengthi++){
if(select.options[i].selected){
str.push(select[i].value)
}
}
alert(str)
}
3、效果演示
<form aciton="../php/register.php" method="post"><p>
<label style="color: #ffffffwidth: 40%text-align: right">所&nbsp&nbsp在&nbsp&nbsp省:</label>
<select style="width:75%" id="s_province" name="s_province" onchange="setVal1(this.options[this.selectedIndex].text)"></select>
</p>
<p>
<label style="color: #ffffffwidth: 40%text-align: right">所&nbsp&nbsp在&nbsp&nbsp市:</label>
<select style="width:75%" id="s_city" name="s_city" onchange="setVal2(this.options[this.selectedIndex].text)"></select>
</p>
<p>
<label style="color: #ffffffwidth: 40%text-align: right">所&nbsp&nbsp在&nbsp&nbsp县:</label>
<select style="width:75%" id="s_county" name="s_county" onchange="setVal3(this.options[this.selectedIndex].text)"></select>
</p>
<input type="hidden" id="shengText" value="" name="shengText"/>
<input type="hidden" id="xianText" value="" name="xianText"/>
<input type="hidden" id="shiText" value="" name="shiText"/>
<script class="resources library" src="http://sandbox.runjs.cn/uploads/rs/267/g3ugugjp/area.js" type="text/javascript"></script>
<script type="text/javascript">_init_area()</script>
<p><button type="submit" class="button1">注 册</button>
</p>
</form>
</div>
</body>
<script type="text/javascript">
var Gid = document.getElementById
var showArea = function(){
Gid('show').innerHTML = "<h3>省" + Gid('s_province').value + " - 市" +
Gid('s_city').value + " - 县/区" +
Gid('s_county').value + "</h3>"
}
Gid('s_county').setAttribute('onchange','showArea()')
</script>
<script language="javascript">
function setVal1(_text)
{
document.getElementById("shengText").value=_text
}
function setVal2(_text)
{
document.getElementById("shiText").value=_text
}
function setVal3(_text)
{
document.getElementById("xianText").value=_text
}
</script>
这样我是能获取到值, 但是只获取到最后一个 县的那个 select值。 试了半个小时。发现是js文件为select赋值的原因。 有没有什么其他办法。 既可以用js文件为select赋值,还能获取到值为文本框赋值。