【问题标题】:Using JQuery to get the value of the unchecked checkboxes in a div使用 JQuery 获取 div 中未选中复选框的值
【发布时间】:2014-12-02 12:40:09
【问题描述】:

我正在尝试从一组未选中的 html 复选框中存储 values。我知道JQuery 没有 :unchecked 选择器,所以我必须反转 :checked 选择器。我已经这样做了,我得到了未选中框的值以及许多其他值,例如null0。我不知道这些是从哪里来的。

这是我的HTML

   <div id="tag_results">
        <ul>
            <li>
                <input type="checkbox" value="29" name="tag" checked=""></input>
                Agriculture
            </li>
            <li>
                <input type="checkbox" value="30" name="tag" checked=""></input>
                Angling
            </li>
            <li>
                <input type="checkbox" value="37" name="tag"></input>
                Animals
            </li>
            <li>
                <input type="checkbox" value="54" name="tag" checked=""></input>
                Arts
            </li>
        </ul>
    </div>

这是我的JQuery

    $('#saveBtn').click(function(e){
                e.preventDefault();
                //Get hidden field values and store in an Array

                $tagArray = [];
                $removeTags = [];

                //Get the Checked Tags and store in an Array(will be added)
                $('#tag_results :checked').each(function(){
                    $tagArray.push(this.value);
                });

                //Get the Unchecked Tags and Store in an Array(will be removed)
                $('#tag_results :not(:checked)').each(function(){
                    $removeTags.push(this.value);
                });

                console.log($removeTags);

                //Make Ajax request to the add_tags script and pass Array as parameter. When response recieved show dialog. 
                //Pass the name, id and type of company with the array of tags to the save_tags.php script. 
                $('#active_tags').load('pages/ajax/save_tags.php', {'removeTags': JSON.stringify($removeTags),'tags': JSON.stringify($tagArray) ,'name': $('#comp_name').val(), 'id': $('#comp_id').val(), 'type': $('#type').val() }, function(response, status, xhr){
                });
   });

这是console.log的结果:

Array [ undefined, 0, 0, 0, "37", 0, "54" ]

这是我用来为复选框生成html 的代码:

$output = "<ul>";
    if(mysql_num_rows($result) > 0){//If results returned (tags found matching letter) so construct list
        while($row = mysql_fetch_array($result)){//while there are rows in the results array add a list item 
            if(in_array($row['name'], $applied_tags)){
                $output .= "<li>" .'<input checked type="checkbox" name="tag" value="'.$row['id'].'">' .$row['name'] .'</li>'.PHP_EOL;
            }else{
                $output .= "<li>" .'<input type="checkbox" name="tag" value="'.$row['id'].'">' .$row['name'] .'</li>'.PHP_EOL;
            }
        }
    }else{//Else there was no matching tags so display an error message to the user. 
        $output .= "<li>No Tags found beginning with the letter " . $letter . "</li>";
    }
    $output .= "</ul>";

有人知道是什么原因造成的吗?当然,根据上面的代码,数组应该包含值 37 和 54...

【问题讨论】:

    标签: javascript php jquery html checkbox


    【解决方案1】:

    试试这个

    $("input:checkbox:not(:checked)")
    

    【讨论】:

    • 成功了,谢谢。为什么我的原始代码不起作用??
    • 它的发生是因为没有提到元素的类型。
    • 好的,非常感谢,3分钟后接受答复。
    【解决方案2】:

    在你的代码中你 did not mention check box or name of checkbox ,所以它 will selected all children of tag_results 并检查所有元素

    $('#tag_results [type=checkbox]:not(:checked)').each(function () {
        $removeTags.push(this.value);
    });
    

    DEMO

    注意:$('#tag_results :not(:checked)') 将选择除选中的所有子元素

    【讨论】:

      猜你喜欢
      • 2012-07-02
      • 2011-01-10
      • 1970-01-01
      • 2013-09-15
      • 2015-01-31
      • 1970-01-01
      • 2015-08-15
      • 2011-10-17
      • 2023-03-18
      相关资源
      最近更新 更多