我的网站上有一个表单,其中有多个同名的复选框:
<span class="selection"><input type="checkbox" name="product" value="Hard Enamel" />Hard Enamel</span>
<span class="selection"><input type="checkbox" name="product" value="Soft Enamel" />Soft Enamel</span>
<span class="selection"><input type="checkbox" name="product" value="Metal Relief" />Metal Relief</span>
<span class="selection"><input type="checkbox" name="product" value="Printed With Epoxy" />Printed With Epoxy</span>
在我的 Javascript 中,我将它们分配给一个变量,并使用 JQuery 提供一个 onclick 函数。有了这个,我仍然可以毫无问题地运行 JQuery 函数,例如products.css("display","none")
有效。
但是当尝试选择产品
中的特定复选框时,它会返回错误。
这是我的 JS:
var products = $("input[name=product]").click(function(){selectProduct()});
products.get(0).css("display","none");
上面的方法不起作用,我收到错误 badges_calc.js:9TypeError: 'undefined' is not a function (evaluating 'products.get(0).css("display","none")')
我该如何解决这个问题?我是个初学者。
干杯
最佳答案
get()
返回 DOM 元素,尝试使用 eq
代替。 DOM 元素没有 css 方法,它是 jquery 对象拥有的。 eq(0)
将为您提供集合中的第一个元素作为 jquery 对象。
products.eq(0).css("display","none");
如果您确实希望隐藏跨度和复选框,则使用:
products.eq(0).closest('span').css("display","none");
否则,只需隐藏复选框,它就会将跨度与文本本身保留在一起。
关于javascript - JQuery .get() 的问题 -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19279674/