[英]Javascript - “is not a function” for parameter added to a function
I have this input: 我有这个输入:
<input type="checkbox" id="test_checkbox" onchange="fffunction(this)" /> Something
After checking/unchecking the checkbox is called the fffunction
function: 选中/取消选中该复选框后,称为fffunction
函数:
function fffunction(checked_value) {
if(checked_value.is(":checked")){
console.log("checked");
} else {
console.log("not checked");
}
}
And this return an error that checked_value.is is not a function
. 这将返回一个错误,表明checked_value.is is not a function
。 How to check if the passed attribute is a function? 如何检查传递的属性是否是一个函数? How to use the parameter? 如何使用参数?
If I do this: 如果我这样做:
function fffunction(checked_value) {
if($('#test_checkbox').is(":checked")){
console.log("checked");
} else {
console.log("not checked");
}
}
Everything's working... How to use the parameter, tho, instead of calling directly the ID HTML element inside the function? 一切正常...如何使用参数tho,而不是直接在函数内调用ID HTML元素?
Thank you in advance. 先感谢您。
You're trying to use a jQuery function on a raw DOM element. 您正在尝试在原始DOM元素上使用jQuery函数。 To do that, you have to wrap it in a jQuery instance first: 为此,您必须先将其包装在jQuery实例中:
function fffunction(checked_value) {
if($(checked_value).is(":checked")){
// ^^-------------^---------------------- note
console.log("checked");
} else {
console.log("not checked");
}
}
That's directly wrapping the element, not going by ID. 那是直接包装元素,而不是按ID。
Of course, there's no reason to use jQuery in this particular case, just use the element's native checked
property: 当然,在这种特殊情况下没有理由使用jQuery,只需使用元素的本机checked
属性即可:
function fffunction(checked_value) {
if(checked_value.checked){
console.log("checked");
} else {
console.log("not checked");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.