Javascript:Can';t使用$(';id';)获取元素


Javascript: Can't get element using $('id')

我想使用$('id')访问对象,但无法成功。

这是我的代码:

function jumptopage(myOffset, mySilder, table) {  // javascript
    ...
    var oPageSlice = $(recordsPageName);  // -> stop
    var myURL = $("jumptourl").value; // -> -> stop
    ...
} 

程序总是停在那两行。recordsPageName为'records_page',并且两者都为"jumptourl"&'records_page'确实存在。我可以使用"var oPageSlice=document.getElementById(recordsPageName);"替换"var oPageSlice=$(recordsPageName);"

它会起作用的。但我还是想找到原因。

p.s.这个问题也可以在下面重现。

在我的php代码中似乎也存在同样的问题。

html[] = '<input type="button" 
onClick="setPerPageRecs($('''.$this->aName['records_page'].''')...'

在这种情况下,当我单击按钮时,不会调用setPerPageRecs$(''''.$this->aName['records_page'].''''似乎不对应到对象。

您需要添加#以按id获取元素,如下所示:

$("#id"); 

查看选择器#ID。它缺少#来选择ID

$("#jumptourl").val();

您错过了#字符来按id获取元素,并使用val()而不是value:

$("#jumptourl").val()
---^

要按类访问,请使用$('.elementClass')

我认为$("jumptourl")是指引用一个id还是一个类?

您需要为类使用.,或为id使用#

例如

$(".jumptourl")$("#jumptourl")