jQuery一个调用几个动作 [英] jQuery one call several actions

查看:111
本文介绍了jQuery一个调用几个动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读jQuery性能的几个指南我发现了这个功能:

Reading several guidelines for jQuery performance I discovered this functionality:

而不是:

$('#legendGallery).draggable({containment:'#container'});
$('#caption').draggable({containment:'#container'});
$('#controls').draggable({containment:'#container'});

执行此操作:

$('#legendGallery, #caption, #controls').draggable({containment:'#container'});

(一次调用jQuery引擎几个动作)

(One call to the jQuery engine several actions applied)

我想将这个概念应用到一个复选框数组:

I want to apply this concept to an array of checkboxes:

<input type="checkbox" class="largecheckbox" name="chk_wms[]" value="m1" />
<input type="checkbox" class="largecheckbox" name="chk_wms[]" value="m2" />
<input type="checkbox" class="largecheckbox" name="chk_wms[]" value="m3" />
<input type="checkbox" class="largecheckbox" name="chk_wms[]" value="m4" />

使用以下代码,我要禁用几个复选框:

With the following code I want to disable several checkboxes:

$('input:checkbox[name="chl_wms\[\]"][value="m1"]', 'input:checkbox[name="chl_wms\[\]"][value="m2"]', 'input:checkbox[name="chl_wms\[\]"][value="m3"]).prop('disabled', true);

但它不工作,没有错误,没有任何操作。

But it's not working, no error and no action applied.

有更好的方法来定义选择器吗?
有一种方法可以使用一个单独的调用来完成该命令。

Is there a better way to define the selectors? Is there a way to do that command using one single call?

谢谢!

推荐答案

您的教师似乎有打字错误。另外,jQuery转义字符是 \\ ,每个选择器应该在一个字符串中。考虑到这一点,应该是:

There appears to be a typo in your selctors. Plus, the jQuery escape char is \\, and each selector should be in one string. With that in mind it should be:

$('input:checkbox[name="chk_wms\\[\\]"][value="m1"], input:checkbox[name="chk_wms\\[\\]"][value="m2"], input:checkbox[name="chk_wms\\[\\]"][value="m3"]').prop('disabled', true);

通过确定它应该可以正常工作。

By fixing that it should work.

一个比属性选择器更好的替代方法是使用类。例如:

An alternative method which would perform better than the attribute selector would be to use classes. Eg:

<input type="checkbox" class="largecheckbox flag-disabled" name="chk_wms[]" value="m1" />
<input type="checkbox" class="largecheckbox flag-disabled" name="chk_wms[]" value="m2" />
<input type="checkbox" class="largecheckbox flag-disabled" name="chk_wms[]" value="m3" />
<input type="checkbox" class="largecheckbox" name="chk_wms[]" value="m4" />    
----
$(".flag-disabled").prop("disabled", "disabled");

或者更好的是使用Ids:

Or even better yet, use Ids:

<input type="checkbox" class="largecheckbox" id="chk_wms[m1]" name="chk_wms[]" value="m1" />
<input type="checkbox" class="largecheckbox" id="chk_wms[m2]" name="chk_wms[]" value="m2" />
<input type="checkbox" class="largecheckbox" id="chk_wms[m3]" name="chk_wms[]" value="m3" />
<input type="checkbox" class="largecheckbox" id="chk_wms[m4]" name="chk_wms[]" value="m4" />    
----
$("#chk_wms\\[m1\\], #chk_wms\\[m2\\], #chk_wms\\[m3\\]").attr("disabled", "disabled");

这篇关于jQuery一个调用几个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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