【问题标题】:disable anchor link '<a>' if one of my six checkboxes are not checked如果未选中我的六个复选框之一,则禁用锚链接'<a>'
【发布时间】:2018-10-09 06:32:39
【问题描述】:

如果我的六 (6) 个复选框中有一个 (1),我如何禁用 锚链接是不是检查?

    var first_option = $('#pid-1590083, #pid-1590090, #pid-1590091, #pid-1590092, #pid-1590093, #pid-1590094');

        $("a").click(function(e) {
       if($("first_option").prop("checked") === false) {
        e.preventDefault(); return false;
       } else {return true;};
});

【问题讨论】:

标签: javascript jquery button disable


【解决方案1】:

您当前的逻辑不起作用,因为您只查看您选择的第一个元素的 checked 属性,而不是所有元素。

要达到您的要求,您可以使用:checked 选择器来获取您提供的选择器中的所有选中元素,然后检查结果的length 属性以查看是否没有。试试这个:

var $first_option = $('#pid-1590083, #pid-1590090, #pid-1590091, #pid-1590092, #pid-1590093, #pid-1590094');

$("#tmp_button-99035").click(function(e) {
  if ($first_option.filter(':checked').length === 0) {
    e.preventDefault();
    alert('Please Choose Collar Colour To Continue');
  };
});

【讨论】:

  • 它不是一个按钮,它是一个要禁用的锚链接。如果六个复选框之一未选中,则禁用锚链接
  • 我找到了这段代码,但无法使用多个复选框' $("a").click(function(e) { if($("#checkBox").prop("checked") === false) { e.preventDefault(); return false; } else { return true; }; }); '
  • 此逻辑也适用于 a 元素。如果您正在苦苦挣扎,那么查看问题的完整示例将有所帮助。您能否编辑问题以包含所有相关代码。
  • 谢谢你,Rory,已经纠正了我的错误并修正了它。
【解决方案2】:

first_option.prop("checked") 将始终检查第一个元素。你要做的就是遍历所有元素来检查

这样

$("#tmp_button-99035").click(function(e) {
    var isChecked = false;

    for (var i = 0; i < first_option.length; i++) {
        if (first_option.eq(i).prop("checked")) {
            isChecked = true;
            break;
        }
    }

    if (!isChecked) {
        alert('Please Choose Collar Colour To Continue');
        e.preventDefault();
    }
    return isChecked;
});

【讨论】:

    【解决方案3】:

    嗯,你的 js sn-p 只检查第一个元素。因此,您还必须跟踪其他复选框以获得正确的结果。

    var first_option = $('#pid-1590083, #pid-1590090, #pid-1590091, #pid-1590092, #pid-1590093, #pid-1590094')
    
    $(document).on('click', '#tmp_button-99035', function (e) {
        if ($(first_option).filter(":checked").length == 0) {
            e.preventDefault();
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多