jquery$notation为文档元素返回null
我正在尝试使用javascript更改ToysRUs地址页上选择框的值。该项目是一个自动填写地址表单的扩展jquery$notation为文档元素返回null,jquery,select,triggers,null,Jquery,Select,Triggers,Null,我正在尝试使用javascript更改ToysRUs地址页上选择框的值。该项目是一个自动填写地址表单的扩展 项目id为billingAddress.address.stateSelect document.getElementById("billingAddress.address.stateSelect") // returns the element. $("#billingAddress.address.stateSelect") // returns null. 我想使用$(“#b
项目id为
billingAddress.address.stateSelect
document.getElementById("billingAddress.address.stateSelect") // returns the element.
$("#billingAddress.address.stateSelect") // returns null.
我想使用$(“#billingAddress.address.stateSelect”).val(“CA”)
来更改选择,但我无法更改,因为它为空
页面正在使用jQuery
(typeof jQuery != 'undefined') // is true
在用反斜杠跳过句点后,我仍然得到null
s=document.getElementById("billingAddress.address.stateSelect").id;
console.log(s);
s= "#"+s.replace( /(:|\.|[|])/g, "\\$1" );
//also tried \\\\ backslashes and \\\ backslashes
console.log(s); //gives billingAddress\\.address\\.stateSelect
console.log($(s)); //is still null
您没有发布完整的代码。我能想到的只有 1:您的代码在select loaded之前运行,本例使用$(function(){your code goes here});
2:您不能在名称中使用点,因为浏览器认为它想告诉它点表示类,而不是名称的一部分。除非jQuery被别名为$,否则$符号不一定有效。尝试使用:
jQuery("#billingAddress.address.stateSelect");
而不是美元符号
编辑:
我没有仔细阅读这个问题。您使用的选择器用于ID,该ID也有两个类名,即“address”和“stateSelect”。我提到的可能仍然是一个问题,但您需要首先确保使用了正确的选择器 在css中,billingAddress.address.stateSelect将选择什么?id为
billingAddress
的元素,类名为address
和stateSelect
,对吗?jquery选择器是css选择器,因此必须对某些特殊字符进行转义,以使用所述特殊字符选择id。是#billingAddress.address.stateSelect
元素的id
?相关:如@KevinB所示,选择器可能会将
作为类混淆:$(“#billingAddress.addres.stateSelect”)
其中getElementById不会执行此操作。如上所述,请跳过句点。请参阅此提琴: