【问题标题】:Check Box In jsp page Using Jquery使用Jquery在jsp页面中的复选框
【发布时间】:2012-12-25 15:55:03
【问题描述】:

我在我的应用程序中使用了复选框,但我遇到了一个问题,尽管我已经努力解决了。

function allChecked(status,set){
    alert(set);
    if(status)
        $('input[type=checkbox]').attr('checked','true');
    else
        $('input[type=checkbox]').attr('checked',''); 
    }

上述函数根据“status”的值选择或取消选择所有复选框。 “set”是一个数字,指定应选择多少个复选框。所以我希望代码根据“set”的值选择复选框。我该怎么做?

【问题讨论】:

  • 你能给我们一个“set”参数的示例值吗?
  • 就像我在页面上使用选择意味着在下拉菜单中我给出的值 10,15,25,100 取决于复选框选择的数量,如果我选择 10 意味着,十复选框选择

标签: jquery jsp checkbox


【解决方案1】:

假设您要选中或取消选中 第一个 N 个复选框(按照它们在 DOM 中出现的顺序),您可以使用 slice 方法来完成此操作: p>

function allChecked(status, set) {
    var checkboxes = $('input[type=checkbox]').slice(0, set);

    if (status) {
        checkboxes.attr('checked', 'checked');
    }
    else {
        checkboxes.removeAttr('checked');
    }
}

演示: http://jsfiddle.net/brianpeiris/Xs9Vc/

【讨论】:

    【解决方案2】:

    编辑:

    对于函数调用,您可以使用以下代码

    function allChecked(){
        if($('#selectall').attr('checked', this.checked))
            $(".case").attr("checked", "checked");
        else
           $(".case").removeAttr("checked"); 
    
        }
    

    示例http://jsfiddle.net/ipsjolly/wUgwz/7/

    你也可以使用 ID 代替调用函数

    $(function(){
    
      // add multiple select / deselect functionality
      $("#selectall").click(function () {
          $('.case').attr('checked', this.checked);
      });
    
      // if all checkbox are selected, check the selectall checkbox
      // and viceversa
      $(".case").click(function(){
    
        if($(".case").length == $(".case:checked").length) {
            $("#selectall").attr("checked", "checked");
        } else {
            $("#selectall").removeAttr("checked");
        }
    
      });
    });​
    

    示例http://jsfiddle.net/ipsjolly/wUgwz/8/

    来源:http://viralpatel.net/blogs/multiple-checkbox-select-deselect-jquery-tutorial-example/

    【讨论】:

      【解决方案3】:

      试试这样的

      function allChecked(status,set){
      alert(set);
      if(status)
          $('input[type=checkbox]:lt('+set+')').attr('checked','true');
      else
          $('input[type=checkbox]:lt('+set+')').attr('checked',''); 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-28
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多