js获取页面元素(下拉框选择)值有关问题
js获取页面元素(下拉框选择)值问题
struts2开发,前台页面有一个下拉框inventorybeo.supplier,对该框进行选择后,js函数searchContactorsAndBank()要用到inventorybeo.supplier的值,该怎么取值呢?
我这样写 $("#inventorybeo.supplier").val(),
alert( "inventorybeo.supplier:"+$("#inventorybeo.supplier").val());
显示这个值为undifined,但是已经点击选择过下拉框了
另外也尝试过$("inventorybeo.supplier").value,均不行
图为谷歌浏览器下js控制台信息,显示下拉框的各个选项是有正确值的;
之前做demo的时候invetorybeo.supplier的名字是supplier,可以正常运行的。因为这是一个struts2的项目,所以命名必须用invetorybeo.supplier,就取不到值了,不知道原因在哪里
我现在只能通过设置var obj1= document.getElementById('inventorybeo.supplier'); var index = obj1.seletctedIndex来获取值,这样可行,但是很不方便
------解决方案--------------------
由于jquery对于获取元素进行了各种情况的封装,#inventorybeo.supplier 会被认为 id为inventorybeo 并且class中包含supplier 的元素,很明显是不存在的,所以你要对于. 做下转义处理,提供如下两种方式
<td width="8%">供应商</td><td width="4%">
<select id="inventorybeo.supplier"
name=inventorybeo.supplier
onchange="searchContactorsAndBank()"
style="width:100%"></select> </td>
struts2开发,前台页面有一个下拉框inventorybeo.supplier,对该框进行选择后,js函数searchContactorsAndBank()要用到inventorybeo.supplier的值,该怎么取值呢?
我这样写 $("#inventorybeo.supplier").val(),
alert( "inventorybeo.supplier:"+$("#inventorybeo.supplier").val());
显示这个值为undifined,但是已经点击选择过下拉框了
另外也尝试过$("inventorybeo.supplier").value,均不行
图为谷歌浏览器下js控制台信息,显示下拉框的各个选项是有正确值的;
之前做demo的时候invetorybeo.supplier的名字是supplier,可以正常运行的。因为这是一个struts2的项目,所以命名必须用invetorybeo.supplier,就取不到值了,不知道原因在哪里
我现在只能通过设置var obj1= document.getElementById('inventorybeo.supplier'); var index = obj1.seletctedIndex来获取值,这样可行,但是很不方便
------解决方案--------------------
由于jquery对于获取元素进行了各种情况的封装,#inventorybeo.supplier 会被认为 id为inventorybeo 并且class中包含supplier 的元素,很明显是不存在的,所以你要对于. 做下转义处理,提供如下两种方式
function searchContactorsAndBank()
{
alert($('#inventorybeo\\.supplier').val());
alert(document.getElementById('inventorybeo.supplier').value);
}