0

我正在使用 ajax 项目。我有一种用户形式,其中包含姓名、地址、邮政编码。在输入姓名、地址或邮政编码时,匹配的行由 ajax 文件显示在 .

所以选择复选框我想做一些进一步的活动。

我的html代码是

Address : <input type="text" name="user_name" id="from_location" value="" class="in_form" />
 <div id="user"></div>

和 jQuery 代码是

$.ajax({
                url: "ajax_user.php",
                data: {
                    address: address,

                 },
                dataType: "html",
                type: "POST",
                success: function(result){
                        $("#user").append(result);
                    }
                })
            }

和 ajax 用户 php 是

$sql= "SELECT * FROM instructor_mst WHERE sex='$sex' AND car_type='$car_type' AND address Like '%$address%' ";
        if (!$sqli=mysql_query($sql)){
            echo mysql_error();
        }
        $num_rows= mysql_num_rows($sqli);
        if($num_rows != 0)
        {?>
        <table border="0" class="form_ins" >
        <?
        while ($row = mysql_fetch_array($sqli)) 
        {

        ?>    
        <tr>
            <td>

            </td>
            <td>
                Name
            </td>   

            <td>
                Address
            </td>



        </tr>
        <tr>
            <td>
                <input type="checkbox" name="select" value"<?php echo $row['id'];?>"> 
            </td>
            <td>
                <?php echo $row['name'];?>
            </td>   

            <td>
                <?php echo $row['address'];?>
            </td>



        </tr>
        </table
        <?}

结果我得到了

复选框 | 用户名 | 地址

现在选择我要提交其他活动的复选框....我不知道我该怎么做...所有答案都将得到认可

4

2 回答 2

2

随着您的复选框被动态添加 -

 $(document).on('change','input[type=checkbox]',function(){
       if($(this).is(':checked')){
         // do something
       }
 });

或者,如果您有复选框的 ID -

$(document).on('change','#checkBoxID',function(){
   if($(this).is(':checked')){
     // do something
   }
});
于 2013-04-09T09:31:16.993 回答
0

您需要使用on动态添加的元素委托点击事件,在您的情况下是复选框

 $(document).on('click','input[name="select"]',function(){
       //this is called when you select the checkbox
       //do your stuff
 })

或将其委托给最近的静态元素

 $('#user').on('click','input[name="select"]',function(){
     //dou your stuff
 });

更新

复选框可能有多个值,因此要获取所有选中的值,您需要遍历这些值或使用 map()

尝试这个

   $('#user').on('click','input[name="select"]',function(){
     var  selectedValue = $("input[name='select']:checked").map(function(n){
            return this.value;
     });
     console.log(selectedValue );  //this will print array in console.
     alert(seletedValue.join(',')); //this will alert all values ,comma seperated
   });
于 2013-04-09T09:37:49.807 回答