【问题标题】:Logic in JQuery, updating with checkboxesJQuery 中的逻辑,使用复选框进行更新
【发布时间】:2014-10-12 19:02:58
【问题描述】:

我所有的代码都可以正常工作,我只是想扩展它以适应所有可能性,知道必须有更好的方法来做到这一点,但不要认为我对 javascript 的了解不够(我刚开始学习)。

基本上,我正在尝试根据我的网络应用程序上的哪些复选框被选中从数据库中调用数据。有四个框:“委员会”、“候选人”、“个人”、“意见”。

我的代码是这样的:

if($('#committees').attr('checked')) {
    $.get("file.pl",
    {
       act: "near",
       format: "raw"
       what: "committees"
    }, NewData);
}
else {
    $.get("file.pl",
    {
       act: "near",
       format: "raw"
       what: ""
    }, NewData);

仅适用于委员会复选框,(其他框的 id 与“what”的表名匹配,并与上述名称匹配。我希望对所有 4 个复选框都进行此项工作,没有必须为每个可能的复选框组合创建大量嵌套语句。

我知道我可以像这样在“什么”类别中列出更多选项:

$.get("file.pl",
        {
           act: "near",
           format: "raw"
           what: "committees, candidates, individuals, opinions"
        }, NewData);

这行得通,但是我知道如何动态更改该字符串以匹配选中的框。感谢您提供的任何帮助!

【问题讨论】:

    标签: javascript jquery database checkbox logic


    【解决方案1】:

    即使您说您的代码有效,我还是建议您使用.is(':checked') 而不是.attr('checked')

    然后您必须为每个复选框分配一个他们都将共享的类,例如.checkbox。你的代码是:

    $('.checkbox').each(function() {
        if($(this).is(':checked')) {
            $.get("file.pl",
                  {
                      act: "near",
                      format: "raw"
                      what: this.id
                  }, NewData);
        } else {
            $.get("file.pl",
                  {
                      act: "near",
                      format: "raw"
                      what: ""
                  }, NewData);
        }    
    });
    

    更新

    我现在意识到像这样的构造确实需要一个闭包才能工作:

    $('.checkbox').each(function() {
        (function( that ) {
            if($(that).is(':checked')) {
                $.get("file.pl",
                  {
                      act: "near",
                      format: "raw"
                      what: that.id
                  }, NewData);
            } else {
                $.get("file.pl",
                  {
                      act: "near",
                      format: "raw"
                      what: ""
                  }, NewData);
            }
        })( this );   
    });
    

    【讨论】:

    • 这不起作用,因为只有最后选择的数据通过,但我想通了,在下面发布了我的答案。感谢您的回复。
    • 我想的也差不多。很高兴你弄清楚了。如果您想一次处理所有checkoxes,更新后的版本可能会有所帮助。
    【解决方案2】:

    你的意思是这样的。使用“this”关键字。
    HTML

    <input type="checkbox" class="file-pl" id="committees" />
    <input type="checkbox" class="file-pl" id="candidates" />
    <input type="checkbox" class="file-pl" id="individuals" />
    <input type="checkbox" class="file-pl" id="opinions" />
    

    JS

    $(".file-pl").click(function(){
        var what = this.checked ? this.id : "";
    
        $.get("file.pl", {
        act: "near",
        format: "raw"
        what: what
        }, NewData);
    });
    

    【讨论】:

      【解决方案3】:

      不幸的是,上述答案并不完全奏效。最终的工作是这样的:

      var what string = "";
      $('.filters').each(function() {
         if($(this).is(':checked')) {
            what string = whatstring+this.id+", ";
           }});
      $.get("file.pl",
          {
             act: "near",
             format: "raw"
             what: whatstring
          }, NewData);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-09
        • 2017-05-19
        • 1970-01-01
        • 1970-01-01
        • 2018-05-01
        • 1970-01-01
        • 2021-11-07
        • 2016-09-26
        相关资源
        最近更新 更多