jQuery this.elem.prop 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14232465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:22:49  来源:igfitidea点击:

this.elem.prop is not a function

jquery

提问by Ras4U

I need to select all cities under states (eg. India) listed. I put a select all:

我需要选择列出的州(例如印度)下的所有城市。我放了一个全选:

<input type="checkbox" class="selectall" /> Select All

Then the below error is appearing:

然后出现以下错误:

this.elem.prop is not a function

this.elem.prop 不是函数

This is my code:

这是我的代码:

jQuery('.selectall').click(function() {
    stop = false;
    jQuery(this).closest('div').nextAll().each( function() {
        elem = jQuery(this).find('input');
        console.log(elem);
        if (stop || elem.hasClass('selectall')) {
            stop = true;
            return;
        }
        else
            elem.prop("checked", true);
    });
});

回答by Rory McCrossan

The prop()method was only added in jQuery 1.6. In previous versions you should use attr().

prop()方法仅在 jQuery 1.6 中添加。在以前的版本中,您应该使用attr().

elem.attr("checked", true);

Note about other answers, elemis already a jQuery object as it was defined on this line: elem = jQuery(this).find('input');, therefore the prop()method would be available if the jQuery version supports it.

请注意其他答案,elem它已经是一个 jQuery 对象,因为它在此行中定义:elem = jQuery(this).find('input');,因此prop()如果 jQuery 版本支持该方法,则该方法将可用。



UPDATE

更新

To toggle the checkbox, use this:

要切换复选框,请使用:

if (stop || elem.hasClass('selectall')) {
    stop = true;
    return;
}
else
    elem.prop("checked", !elem.is(':checked')); // toggle the checkbox

回答by VHS

Your JQuery version must be older than v1.6. See the following JQuery documentation link that states that the "prop" function was only added from v1.6 onwards.

您的 JQuery 版本必须早于 v1.6。请参阅以下 JQuery 文档链接,该链接指出“prop”函数仅从 v1.6 开始添加。

http://api.jquery.com/prop/

http://api.jquery.com/prop/

So the solution to your problem is to use "attr" function instead of "prop".

因此,解决您的问题的方法是使用“attr”函数而不是“prop”。

Apparently, attr retrieves attributes (which are the initial String values of the HTML elements) and prop retrieves properties (which could be the DOM manipulated values and could be of any type such as string, boolean, etc). Read the following Stackoverflow link for more detailed explanation on the difference between the two.

显然,attr 检索属性(它们是 HTML 元素的初始字符串值),prop 检索属性(可以是 DOM 操作值,可以是任何类型,例如字符串、布尔值等)。阅读以下 Stackoverflow 链接以获取有关两者之间差异的更详细说明。

.prop() vs .attr()

.prop() 与 .attr()