未捕获的TypeError:对象[object HTMLInputElement]没有方法'prop' [英] Uncaught TypeError: Object [object HTMLInputElement] has no method 'prop'

查看:161
本文介绍了未捕获的TypeError:对象[object HTMLInputElement]没有方法'prop'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试通过jquery设置单选按钮时..它给我一个错误

when i am trying set radio button checked through jquery.. it gives me an error

未捕获的TypeError:对象[object HTMLInputElement]没有方法 'prop'

Uncaught TypeError: Object [object HTMLInputElement] has no method 'prop'

var sub="twoWheeler";
if (vehicleType == sub) {
       alert("asdasd");
       window.opener.document.getElementById("radios1").prop("checked");
}else{

};

推荐答案

getElementById返回DOM元素引用(在您的情况下为 .prop() 属于 jQuery对象的方法.

getElementById returns a DOM element reference (in your case an HTMLInputElement), that is, it doesn't have a .prop() method which belongs to jQuery objects.

此外,您的代码没有副作用,因为使用单个参数调用.prop()仅仅是一个吸气剂,并且您没有将其分配给任何东西.要设置元素的checked属性,您可以使用:

Besides that, your code would have no side effect as calling .prop() with a single parameter is merely a getter and you're not assigning it to anything. To set the element's checked property you can use:

window.opener.document.getElementById("radios1").checked = true;

上面是使用Pure JS的,问题是用jQuery标记的,但此时使用它只会使您在处理2个不同的窗口对象时变得更加困难.

The above is with Pure JS, the question is tagged with jQuery but using it at this point would only make it harder as you're working through 2 different window objects.

假设jQuery与#radios1包含在同一页面中,您可以使用:

Assuming jQuery is included in the same page as #radios1 you could use:

window.opener.$('#radios1').prop('checked', true);

演示(可能需要启用弹出窗口)

Demo (may need popups enabled)

或者,如果jQuery仅包含在运行上述代码的页面中,则可以使用上下文选择器:

Or if jQuery is included only in the page from where you're running the code above, you can use a context selector:

$('#radios1', window.opener.document).prop('checked', true);

演示(可能需要启用弹出窗口)

Demo (may need popups enabled)

但是,在这种情况下,IMO使jQuery变得更加令人困惑.如果两个页面都包含jQuery,则两者都可以使用.纯JS不仅速度更快,而且对我来说似乎更干净.

But jQuery just makes things more confusing in this case IMO. If you have jQuery in both pages, both would work. The pure JS one is not only faster but also seems cleaner to me.

这篇关于未捕获的TypeError:对象[object HTMLInputElement]没有方法'prop'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆