使用JQuery获取div中未选中的复选框的值 - javascript

我正在尝试从一组未选中的values复选框中存储html。我知道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 ...

参考方案

试试看

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

当<form>'.submit'函数被覆盖时(使用Ajax)将数据获取到php吗? - javascript

我已覆盖此网页上表单的.submit函数,因为该网页已加载在“ index.php”中的#mainContent内,并且我希望“提交”按钮仅替换此#mainContent。我正在尝试将数据从此表单获取到.php文件,以便对数据库进行查询(或简单地回显已填充的变量,以指示已传递数据)。我是AJAX的新手。有人可以向我解释如何将数据传递到.php文件,或者指向要…

使用php重新加载内容 - javascript

在对网站进行编程时,我以前使用过此代码,它可以完美工作,但是现在当我想使用一些Flash部件时,每次单击链接时,它都会重新加载所有网站。源代码: <!DOCTYPE html> <html> <head> <title>Hot King Staff</title> <meta charset=…

用jQuery填充模式形式 - javascript

我正在将订单表从数据库绘制到datatables(jquery插件)中。我要在每笔最后一笔交易或每笔交易中增加付款。问题是,如何获取单击添加付款按钮以添加付款的行的订单ID。其次,当点击addpayment时,它会弹出一个带有字段或订单号的模态表单。我想用在td中找到的订单ID填充该字段,并使其不可编辑或隐藏,但在提交模态表单时将其发布到服务器。表格和模式表…

保留文本区域的数据或值,然后选择输入 - javascript

通过$ _POST提交表单时,输入文件值仍然保留。像这样: if($_POST){ $error = false; if(!$post['price']){ $error = true; $error_note['price'] = "*Should not be empty"; } if($err…

混合码错误 - javascript

好的,我在网站上为用户个人资料提供了一个基本的多合一页面,如下所示:<?php if($_GET['p']=='pb'){ echo '<p>pb</p>'; }elseif($_GET['p']=='example2'){ ec…