【问题标题】:Jquery - check if checkboxes are checked one at a timeJquery - 检查复选框是否一次检查一个
【发布时间】:2010-08-05 23:02:33
【问题描述】:

基本上我需要做的是通过一系列的复选框,如果它被选中,我需要使用 ajax 提交它的值。现在我似乎无法找到一种方法来一次遍历每个复选框。这是我页面上的代码。

$(document).ready(function(){
$('#submit').click(function(){

});
});

<?php
/**
 * Include vB core.
 */ 
require_once('./global.php');
require_once(DIR . '/includes/functions_bigthree.php');
require_once(DIR . '/includes/functions_forumlist.php');

/**
 * Grab the divisions from the database.
 */
$query = $db->query_read("SELECT division FROM rollcall_divisions");
while($array = $db->fetch_array($query)){
echo "<input type='checkbox' value=".$array[division].">".$array[division]."<br>";
}
?>

<input type="submit" id="submit">

【问题讨论】:

  • 相关部分代码你提交完整了吗?

标签: php jquery checkbox


【解决方案1】:

您可以像这样使用:checkbox 过滤器选择器和each

$('#submit').click(function(){
  $(':checkbox').each(function(){
     if ($(this)).is(':checked') {
       // your code....
     }
  });
});

:checkbox 定位页面上的所有复选框,然后each 用于迭代每个复选框。稍后在条件is:checked 过滤器选择器一起使用以了解这些复选框中的哪些实际被选中。

【讨论】:

    【解决方案2】:

    您可以将:checked selector 用于您的复选框,并使用each() 方法为它们提交ajax 请求:

    $('input[type=checkbox]:checked').each(function(){
        // submit your ajax request        
    });
    

    要实际提交 ajax 请求,jQuery 有 $.ajax() 方法。

    【讨论】:

    • 您使用的是收音机而不是复选框,但我明白了。
    【解决方案3】:
    $('#yourform').find(':checkbox').each( function() {
        // do ajax stuff on $(this)
    });
    

    【讨论】:

      猜你喜欢
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多