检查从 JQuery Load 加载的复选框

发布于 2024-08-18 09:19:56 字数 618 浏览 3 评论 0原文

我正在使用 Jquery 和 load 函数将一组复选框加载到 div 中。我的代码是

$("#cb-list").load('www.someurl.com');

www.someurl.com 返回一堆复选框的 html,例如

<input type="checkbox" value="sddfdsf" name="cb[]" id="a1" />
<input type="checkbox" value="sddfdsf" name="cb[]" id="b2" />

我想浏览已返回的复选框,然后根据参数检查其中一些复选框(我想在加载复选框的页面上执行此操作而不是正在加载的页面)。

我该如何使用 jQuery 来解决这个问题?例如,我可能会返回 3 个复选框,其 ID 为 a1、b2、c3,我想检查 b2 和 c3。我通常会这样做,

$("#b2").attr("checked") = 'checked';
$("#c3").attr("checked") = 'checked';

但这不起作用,我假设这是因为复选框是从外部链接加载的。

谢谢

I am loading a set of checkboxes into a div using Jquery and the load function. My code is

$("#cb-list").load('www.someurl.com');

where www.someurl.com returns html for a bunch of checkboxes e.g.

<input type="checkbox" value="sddfdsf" name="cb[]" id="a1" />
<input type="checkbox" value="sddfdsf" name="cb[]" id="b2" />

I want to go through the checkboxes that have been returned and then check some of them based on parameters (I want to do this on the page that loads the checkboxes and not the page which is being loaded).

How would I go about this using jQuery? So for example I might have 3 checkboxes returned with IDs a1, b2, c3 and I would like to check b2 and c3. I would normally do

$("#b2").attr("checked") = 'checked';
$("#c3").attr("checked") = 'checked';

But this does not work and I am assuming this is because the checkboxes are being loaded from an external link.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

超可爱的懒熊 2024-08-25 09:19:56

假设您的 AJAX 调用返回一个包含上面发布的 HTML 的字符串,那么您需要将该字符串加载到 jQuery 变量中:

//put html into a string
var myCheckboxes = $("#cb-list").load('www.someurl.com');
/*... possibly perform some validations here ... */
var $myCheckboxes = $(myCheckboxes);

然后设置为单独检查:

$myCheckboxes.filter("#b2").attr('checked', true);
$myCheckboxes.filter("#c3").attr('checked', true);

或使用 .each() 设置全部:

$myCheckboxes.each(function(){
    $(this).attr('checked', true);
});

我想你会想将它附加到某些东西上。这里我们用 id ='MyDiv' 附加到一个 div

$myCheckboxes.appendTo("#MyDiv");

Assuming your AJAX call returns a string with the HTML you posted above, then you'll need to load that string into a jQuery variable:

//put html into a string
var myCheckboxes = $("#cb-list").load('www.someurl.com');
/*... possibly perform some validations here ... */
var $myCheckboxes = $(myCheckboxes);

Then set to checked individually:

$myCheckboxes.filter("#b2").attr('checked', true);
$myCheckboxes.filter("#c3").attr('checked', true);

Or set all using .each():

$myCheckboxes.each(function(){
    $(this).attr('checked', true);
});

I guess then you'll want to append it to something. Here we append to to a div with id ='MyDiv'

$myCheckboxes.appendTo("#MyDiv");
靑春怀旧 2024-08-25 09:19:56

它应该是 $("#b2").attr('checked', true);

it should be $("#b2").attr('checked', true);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文