2014-12-02 150 views
0

我試圖從一組未選中的html複選框中存儲values。據我所知,JQuery沒有:未經檢查的選擇器,所以我必須反轉:檢查選擇器。我已經完成了這一步,並且獲取未經檢查的框的值以及其他一些值,如null0。我不知道這些來自哪裏。使用JQuery獲取div中未選中複選框的值

這裏是我的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 ...

回答

2

嘗試了這一點

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

工作,謝謝。爲什麼我的原始代碼不工作? – Javacadabra 2014-12-02 12:47:08

+1

它發生是因爲沒有提及元素的類型。 – 2014-12-02 12:49:47

+0

非常感謝你,會在3分鐘內接受回答。 – Javacadabra 2014-12-02 12:51:47

1

在你的代碼did not mention check box or name of checkbox,所以它瓦特ill selected all children of tag_results,並檢查所有元素

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

DEMO

注:$('#tag_results :not(:checked)')將選擇所有兒童元素除外檢查