JQuery – 按属性选择?

这适用于Opera,但显然没有别的。

HTML

  

JS

 $("input[name=iphone]").attr('checked', 'checked'); $("input[name=photocart]").attr('checked', 'checked'); $("input[name=iphone]").attr('disabled', 'disabled'); $("input[name=photocart]").attr('disabled', 'disabled'); 

我也试过以下无济于事。

 $("input[name|=iphone]").attr('checked', 'checked'); $("input[name|=photocart]").attr('checked', 'checked'); $("input[name|=iphone]").attr('disabled', 'disabled'); $("input[name|=photocart]").attr('disabled', 'disabled'); $("[name=iphone]").attr('checked', 'checked'); $("[name=photocart]").attr('checked', 'checked'); $("[name=iphone]").attr('disabled', 'disabled'); $("[name=photocart]").attr('disabled', 'disabled'); $("[name|=iphone]").attr('checked', 'checked'); $("[name|=photocart]").attr('checked', 'checked'); $("[name|=iphone]").attr('disabled', 'disabled'); $("[name|=photocart]").attr('disabled', 'disabled'); 

没有任何喜悦。 关于如何实现这一目标的任何想法?

编辑: – Epic在复制HTML SORRY时失败了!

您可以这样指定:

 $('input[name=hosting][checked]') 

有关详细信息,请参阅Jquery多选择器页面 。

属性selector [name=val]匹配具有属性名称的所有元素,其值正好是val 。 与此相反, [name|=val]匹配具有属性名称的所有元素,该属性名称的值正好是“ val ”或以“ val ”开头,后面紧跟“ - ”。 后者更倾向于与语言代码一起使用(例如[lang|=en]匹配lang="en-US" )。 因此,使用前一个选择器进行精确匹配。

首先,您有两个名为“hosting”的元素,但您没有使用“input [name = hosting]”。

你的HTML:

   

JS( 应该工作):

 $('input[name=iphone]').attr('checked', true); $('input[name=photocart]').attr('checked', true); $('input[name=iphone]').attr('disabled', true); $('input[name=photocart]').attr('disabled', true); 

请记住,如果您有多个名为“iphone”或“photocart”的元素,jQuery也会更改其属性。

对属性值使用truefalse而不是’checked’或’disabled’。 记住,你正在操纵DOM,所以它与

 //for each one $("input[name=photocart] :checked").val(); $("input[name=iphone] :checked").val(); //general, used with an event result = $(this).is(":checked"); //general, used with a css class in common (add a class="checkable" to your target inputs) $(".checkable:checked").each(function() { result += this.value; } 

请注意,您也可以基本执行以下操作:

 if ($("input[name=iphone]")[0].checked) // returns true or false depending on the value if $("input[name=photocart]")[0].checked) if ($("input[name=iphone]")[0].disabled) // returns boolean value if ($("input[name=photocart]")[0].disabled) $("input[name=iphone]")[0].checked = true;// sets the check to true (checked) $("input[name=photocart]")[0].attr('checked', true); $("input[name=iphone]")[0].disabled = false;// enables the input control $("input[name=photocart]")[0].attr('disabled', true); 

另一个很酷的技巧,将值设置为目前不是像toogle的那样:

 $("input[name=iphone]")[0].checked = !$("input[name=iphone]")[0].checked; 

编辑:示例将所有选中的框设置为未选中:

 $("input:checked")[0] = false;// single $('input:checked').each(function() { this.checked = true; }); 

注意如果你给他们一个名为“mycheckbox”的课程更简单:

 var toggle_click = false; $(function() { $('.mycheckbox').click(function() { $('.mycheckbox').each().checked = !this.checked; toggle_click = !toggle_click; }); }); 

如果选中,请取消选中:

 $('.cptIcdCheckbox:checked').each(function() { this.checked = !this.checked; }); 

我使用这种技术,如果我有一个复选框列表和一个“标题”复选框以取消选中它们或根据“组标题”复选框值和它的点击和/或更改事件检查它们。