三个复选框,必须选中一个才能启用提交按钮,需要 Jquery 吗?

发布于 2024-08-30 09:41:27 字数 1243 浏览 8 评论 0原文

我有 1-3 个复选框,默认情况下它们全部被禁用。为了使提交按钮处于活动状态,必须至少选中复选框。有人可以帮我用 jquery 片段来实现这一目标吗?我的标记如下所示,该网站使用 jquery 1.42。请并谢谢你!

<form action="/cart/add" method="post" id="pform">
  <h3 class="goudy">Make your selection:</h3>
  <ul id="variants">
    <li>
        <input type="checkbox" name="id[]" value="39601622" id="radio_39601622" style="vertical-align: middle;" class="required" />
        <label for="radio_39601622[]">$38.00 - Original Antique Photo</label>
    </li>

    <li>
        <input type="checkbox" name="id[]" value="39601632" id="radio_39601632" style="vertical-align: middle;" class="required" />
        <label for="radio_39601632[]">$8.99 - SCAN</label>
    </li>

    <li>
        <input type="checkbox" name="id" value="39777962" id="radio_39777962" style="vertical-align: middle;" class="required" />
        <label for="radio_39777962">$2.99 - Rigid Sleeve</label>  
    </li>
  </ul>

  <div class="buttons clearfix">
    <input type="image" src="../images/add-to-cart.png" name="add" value="Add to Cart" id="add" class="send-to-cart" />
  </div>    
</form>

I have between 1-three checkboxes and by default they are all disabled. In order for the submit button to be active ove checkbox minumum must be selected. Can someone help me with a jquery snippet to achieve this? My markup looks like this and the site used jquery 1.42. Please and thankyou!

<form action="/cart/add" method="post" id="pform">
  <h3 class="goudy">Make your selection:</h3>
  <ul id="variants">
    <li>
        <input type="checkbox" name="id[]" value="39601622" id="radio_39601622" style="vertical-align: middle;" class="required" />
        <label for="radio_39601622[]">$38.00 - Original Antique Photo</label>
    </li>

    <li>
        <input type="checkbox" name="id[]" value="39601632" id="radio_39601632" style="vertical-align: middle;" class="required" />
        <label for="radio_39601632[]">$8.99 - SCAN</label>
    </li>

    <li>
        <input type="checkbox" name="id" value="39777962" id="radio_39777962" style="vertical-align: middle;" class="required" />
        <label for="radio_39777962">$2.99 - Rigid Sleeve</label>  
    </li>
  </ul>

  <div class="buttons clearfix">
    <input type="image" src="../images/add-to-cart.png" name="add" value="Add to Cart" id="add" class="send-to-cart" />
  </div>    
</form>

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

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

发布评论

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

评论(5

爱的那么颓废 2024-09-06 09:41:27

如果没有检查任何内容,另一种禁用表单按钮的方法!

$(function() { 
    $('#add').attr('disabled','disabled');
    $('#pform input:checkbox').bind('click',function() {
        if($(this).is(':checked')) {
         $('#add').removeAttr('disabled');
        } else {
         $('#add').attr('disabled','disabled');
        }
   });
});​

Another way with disabled form button if nothing checked!

$(function() { 
    $('#add').attr('disabled','disabled');
    $('#pform input:checkbox').bind('click',function() {
        if($(this).is(':checked')) {
         $('#add').removeAttr('disabled');
        } else {
         $('#add').attr('disabled','disabled');
        }
   });
});​
美羊羊 2024-09-06 09:41:27

假设提交按钮一开始就被禁用(经过测试):

$(document).ready(function() {

    // disable submit once the DOM is ready
    $("input.send-to-cart").attr("disabled", "disabled");
    $("input.required:checkbox").click(function() {
        $("input.send-to-cart").attr("disabled", !$("input.required:checkbox:checked").length);
    });
});

Assuming the submit button is disabled to begin with (tested):

$(document).ready(function() {

    // disable submit once the DOM is ready
    $("input.send-to-cart").attr("disabled", "disabled");
    $("input.required:checkbox").click(function() {
        $("input.send-to-cart").attr("disabled", !$("input.required:checkbox:checked").length);
    });
});
岁月静好 2024-09-06 09:41:27

另一种方式:

pform = null;
numberSelected = 0;

$(function(){
    pform = $("#pform");
    $("input[type=checkbox]", pform).click({
        if ($(this).is(":selected"))
            numberSelected++;
        else
            numberSelected--;

        if (numberSelected > 0)
            $('#add').removeAttr('disabled');
        else
            $('#add').attr('disabled', true);
    });
});

Another way:

pform = null;
numberSelected = 0;

$(function(){
    pform = $("#pform");
    $("input[type=checkbox]", pform).click({
        if ($(this).is(":selected"))
            numberSelected++;
        else
            numberSelected--;

        if (numberSelected > 0)
            $('#add').removeAttr('disabled');
        else
            $('#add').attr('disabled', true);
    });
});
慈悲佛祖 2024-09-06 09:41:27

试试这个:

$(function(){
  if ($('input[type="checkbox"]:checked').length > 0)
  {
       // submit the form now
  }
});

Try this:

$(function(){
  if ($('input[type="checkbox"]:checked').length > 0)
  {
       // submit the form now
  }
});
旧瑾黎汐 2024-09-06 09:41:27

我需要同样的功能,但在阅读了 aSeptik 的解决方案后,我很怀疑,因为它似乎在选中复选框时启用该按钮,并在未选中复选框时禁用该按钮。我对代码的阅读表明,只要您仅通过选中和取消选中一个复选框来测试它,该解决方案似乎就可以工作 - 尝试选中一个复选框,然后再选中第二个,然后取消选中其中一个,只留下一项被选中——即使您的复选框之一被选中,您也会看到您的按钮被禁用。我测试并确认确实是这样。

I needed this same functionality, but after reading aSeptik's solution I was suspicious, since it seems to enable the button any time a checkbox gets checked and disabled the button any time a checkbox gets unchecked. My reading of the code suggested that the solution would appear to work as long as you only test it by checking and unchecking one checkbox -- try checking one checkbox, then a second, then unchecking one of them, leaving only one checked -- you will see your button disabled even though one of your checkboxes is checked. I tested and confirmed that this is the case.

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