dongxing2710 2014-12-02 12:40
浏览 214


I'm trying to store the values from a set of unchecked html checkboxes. I understand that JQuery does not have an :unchecked selector so I must invert the :checked selector. I've done this and I am getting the values of the unchecked boxes as well as a number of other values such as null and 0. I don't know where these are coming from.

Here is my HTML:

   <div id="tag_results">
                <input type="checkbox" value="29" name="tag" checked=""></input>
                <input type="checkbox" value="30" name="tag" checked=""></input>
                <input type="checkbox" value="37" name="tag"></input>
                <input type="checkbox" value="54" name="tag" checked=""></input>

Here is my JQuery:

                //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(){

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


                //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){

this is the result of console.log:

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

This is the code I use to generate the html for the checkboxes:

$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;
                $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>";

Does anyone have any clue what could be causing this? Surely based on the above code the array should contain the values 37 and 54...

  • 写回答

2条回答 默认 最新

  • duankuiyu4618 2014-12-02 12:44

    Try this out

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
  • duangu1645 2014-12-02 12:45

    In your code you did not mention check box or name of checkbox , so it will selected all children of tag_results and check the all element

    $('#tag_results [type=checkbox]:not(:checked)').each(function () {


    NOTE: $('#tag_results :not(:checked)') will select all children element except checked




  • ¥15 TensorFlow深度学习拓展项目
  • ¥20 springboot博客系统
  • ¥15 关于#c++#的问题:在A和B类中应该怎么定义复制构造函数来避开unordered_set复制构造的问题(相关搜索:头文件)
  • ¥15 MICE包多重插补后数据集汇总导出
  • ¥15 一道算法分析问题(关于3-MSAT)
  • ¥15 C++ FLUENT 化学反应速率 编写困难
  • ¥15 Python嵌套交叉验证
  • ¥15 linuxkit+elasticsearch
  • ¥15 兄得萌6.13do题😭😭大一小东西的work
  • ¥15 投不到原始数据,gdal投影代码