当复选框分配有 jQuery 时,勾选不再起作用

发布于 2025-01-06 17:19:54 字数 789 浏览 4 评论 0原文

我的其中一个页面上的复选框遇到了一个奇怪的问题。基本上,我分配了一些 jQuery(如下),这样当单击它时,下面的字段将填充与上面相同的信息(与计费信息类似)但是,当将此 jQuery 分配给它时,勾号无法显示填写复选框。

我尝试了各种方法来手动勾选复选框,但无济于事,有人可以解释这个问题吗?

$('#sameasbilling').toggle(function() {

    //assign value of billing firstname text input to variable 'firstname'
    var firstname = $('input[name=billingfirstnames]').val();

    //insert value of 'firstname' into delivery text input
    $('input[name=deliveryfirstnames]').val(firstname);

    $('#sameasbilling').attr("checked", checked);

},function() {
    $('input[name=deliveryfirstnames]').val("");
});

我尝试过的 jQuery(加上这两行的许多变体)让自动收报机手动工作如下,但它们都没有工作。

$('#sameasbilling').attr("checked", checked);

$('#sameasbilling').prop("checked", true);

提前致谢。担。

I'm having a strange problem with a checkbox on one of my pages. Basically I have a bit of jQuery ( below ) assigned to it so that when it is clicked the fields below fill up with the same info as above ( same as billing info kinda thing ) However when this jQuery is assigned to it the tick fails to fill the checkbox.

I have tried various methods to get the checkbox to tick manually but to no avail, could anyone shed some light on this problem?

$('#sameasbilling').toggle(function() {

    //assign value of billing firstname text input to variable 'firstname'
    var firstname = $('input[name=billingfirstnames]').val();

    //insert value of 'firstname' into delivery text input
    $('input[name=deliveryfirstnames]').val(firstname);

    $('#sameasbilling').attr("checked", checked);

},function() {
    $('input[name=deliveryfirstnames]').val("");
});

The jQuery I have tried ( plus many variants of those two lines ) to get the ticker to work manually is below, neither of them have worked.

$('#sameasbilling').attr("checked", checked);

$('#sameasbilling').prop("checked", true);

Thanks in advance. Dan.

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

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

发布评论

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

评论(1

一页 2025-01-13 17:19:54

在这种情况下,您使用的 toggle() 方法不正确,请改用 click() 。试试这个:

$('#sameasbilling').click(function() {
    if ($(this).is(":checked")) {
        //assign value of billing firstname text input to variable 'firstname'
        var firstname = $('input[name=billingfirstnames]').val();

        //insert value of 'firstname' into delivery text input
        $('input[name=deliveryfirstnames]').val(firstname);
    }
    else {
         $('input[name=deliveryfirstnames]').val("");
    }
}

Your use of the toggle() method is incorrect in this instance, use click() instead. Try this:

$('#sameasbilling').click(function() {
    if ($(this).is(":checked")) {
        //assign value of billing firstname text input to variable 'firstname'
        var firstname = $('input[name=billingfirstnames]').val();

        //insert value of 'firstname' into delivery text input
        $('input[name=deliveryfirstnames]').val(firstname);
    }
    else {
         $('input[name=deliveryfirstnames]').val("");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文