2012-04-18 69 views
0

我已經在這裏學習所有的答案(https://stackoverflow.com/a/2191026),但建議由@davydepauw和@emeraldjava不工作...下面的代碼不會選擇,即使最清晰的代碼/取消選擇PHP代碼中的框。JavaScript和PHP - 檢查,並取消所有複選框

echo "<form action=$fileName method='post'>"; 
... 
<script language='JavaScript'> 
    $('#select-all').click(function(event) { 
    if(this.checked) { 
     // Iterate each checkbox 
     $(':checkbox').each(function() { 
     this.checked = true;       
     }); 
    } 
    else { 
     // Iterate each checkbox 
     $(':checkbox').each(function() { 
     this.checked = false; 
     }); 
    } 
    }); 
</script>"; 
... 
// This should select/deselect all checkboxes below: 
echo "<input type='checkbox' name='select-all' id='select-all' />"; 
... 
// The below is in the WHILE loop fetching data from MySQL: 
echo "<input type='checkbox' name='IndustryID' value='" . $row['IndustryID'] . "'>"; 
... 
</form> 

對於@DavidThomas要求,下面是渲染代碼:

<body> 
<script language='JavaScript'> 
    $('#select-all').click(function(event) { 
    if(this.checked) { 
     // Iterate each checkbox 
     $(':checkbox').each(function() { 
     this.checked = true;       
     }); 
    } 
    else { 
     // Iterate each checkbox 
     $(':checkbox').each(function() { 
     this.checked = false; 
     }); 
    } 
    }); 
</script> 
... 
<form action=XXX.php method='post'> 
... 
<input type='checkbox' name='select-all' id='select-all' /> 
... 
<input type='checkbox' name='IndustryID' value='3'> 
... 
<input type='checkbox' name='IndustryID' value='5'> 
... 
<input type='checkbox' name='IndustryID' value='148'> 
... 
</form> 
</body> 
+0

你能張貼在jsfiddle.net一個精簡的例子嗎? – Arno 2012-04-18 15:02:34

+0

你的PHP是無關的jQuery和Javascript一般,請出示呈現的HTML,而不是服務器端腳本。 – 2012-04-18 15:03:00

+0

看看這裏:http://api.jquery.com/live/,我覺得你沒有得到的結果,因爲元素創建你的JavaScript之前 – Gerep 2012-04-18 15:04:53

回答

6

你必須把一切的document.ready事件中這樣的,否則代碼運行時的元素不存在,有是沒有元素附加和使用正確的腳本標記

<script type="text/javascript"> 
    $(function(){ 

    $('#select-all').click(function(event) { 
     if(this.checked) { 
      // Iterate each checkbox 
      $(':checkbox').each(function() { 
      this.checked = true;       
      }); 
     } 
     else { 
      // Iterate each checkbox 
      $(':checkbox').each(function() { 
      this.checked = false; 
      }); 
     } 
     }); 
    }) 
</script> 
2

這是因爲你在jQuery代碼後添加複選框。

改變你的JavaScript代碼來此

<script language='JavaScript'> 
$(document).ready(function(){ 
     $('#select-all').click(function(event) { 
     if(this.checked) { 
      // Iterate each checkbox 
      $(':checkbox').each(function() { 
      this.checked = true;       
      }); 
     } 
     else { 
      // Iterate each checkbox 
      $(':checkbox').each(function() { 
      this.checked = false; 
      }); 
     } 
     }); 
}); 
    </script>"; 

或添加JavaScript中顯示覆選框

+0

感謝您的回答。不幸的是,它不起作用。我之前和之後放置了代碼,之後放了我的代碼。它們都不起作用。如果沒有任何區別,我在Ubuntu下的Firefox 11.0上。 – mrinterested 2012-04-18 15:16:17

0
<script type="text/javascript"> 
    $(function(){ 

    $('#select-all').click(function(event) { 
      $(':checkbox').each(function() { 
      $(this).attr('checked', !checkbox.attr('checked'));     
      }); 
     }); 

    }) 
</script> 

未測試在所有...剛剛來到我的腦海

0
var selectAll = false; 

    if(selectAll) { 
     $('input:checkbox').removeAttr('checked'); 
     selectAll = false; 
    } 

    else { 
     $('input:checkbox').attr('checked', 'checked'); 
     selectAll = true; 
    } 
-1

你不能用這個..

$(function(){ 

// add multiple select/deselect functionality 
$("#selectall").click(function() { 
     $('.case').attr('checked', this.checked); 
}); 

// if all checkbox are selected, check the selectall checkbox 
// and viceversa 
$(".case").click(function(){ 

    if(!$(".case:not(:checked)").length) 
    { 
     $("#selectall").attr("checked", "checked"); 
    } else { 
     $("#selectall").removeAttr("checked"); 
    } 

}); 
    }); 

那麼這就是我如何構造我的PHP ..

<input type='checkbox' id='selectall'/> 
$select = mysql_query ("SELECT * FROM tblname") OR DIE (mysql_error()); 
while ($row3=mysql_fetch_array($select_orders2)){ 
    $idd = $row3['id']; 
<input type='checkbox' class='case' name='checkbox[]' value='".$idd."'> 
} 

在這段代碼。所有由sql檢索到的數據都將與類案例一起迭代。希望這會有所幫助。

相關問題