我的问题是,我想在运行时检索复选框 ID 并在以后将它们用于其他目的。 但是检索到的 id 被读取为对象。
我的代码是:
// Following code gives id of checkbox which contains myCheckbox as its id.
var myCheckbox= $('input[id$=myCheckbox]')[0].id;
// and Now I want to check if that checkbox is checked with following code:
if ($(myCheckbox).is(':checked'))
return 1;
else
return 0;
但是这里 myCheckbox id 被读取为对象而不是 id,因此总是在 else 条件下输入并返回 0。当我直接输入复选框的 id 时,这段代码有效。
if ($('#ctl001_myCheckbox').is(':checked'))
return 1;
else
return 0;
它不应该这么复杂,我一直在使用 Javascript 但对 JQuery 是新手。
最佳答案
您获得的 ID 是正确的,但是 jQuery 选择器需要 #
符号,这与 CSS 选择器的方式非常相似。您需要将 #
字符添加到您的选择器中:
if ($('#'+myCheckbox).is(':checked'))
return 1;
else
return 0;
关于javascript - JQuery 在运行时从 Checkbox 中检索 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14424472/