简体   繁体   English

使用由Ajax创建的复选框

[英]Working with checkbox which is creating by Ajax

I am working with ajax project. 我正在使用ajax项目。 I have a form of user in with name, address , postcode . 我有一个用户名,地址,邮政编码。 on entering the name, address or postcode the matching rows is shown by ajax file in . 在输入匹配行的名称,地址或邮政编码时,由ajax文件显示。

so on select the checkbox i want to do some further activity. 所以选择我想要做进一步活动的复选框。

my html code is 我的HTML代码是

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

and jQuery code is 和jQuery代码是

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

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

and ajax user php is 和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
        <?}

in result i got like 结果我喜欢

checkbox | 复选框| user name | 用户名| address 地址

now on selecting the checkbox i want to submit for other activity.... I am not getting how can i do this...All answer will be appreicieted 现在选择我要为其他活动提交的复选框....我没有得到我怎么能这样做...所有的答案将是appreicieted

As your checkbox being dynamically added - 随着您的复选框动态添加 -

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

Or if you have an ID of your checkbox - 或者如果你有一个复选框的ID -

$(document).on('change','#checkBoxID',function(){
   if($(this).is(':checked')){
     // do something
   }
});

you need to delegate the click event using on for dynamically added element which is checkbox in your case 您需要使用on来委托click事件来动态添加元素,这是您案例中的复选框

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

or delegating it to the closest static element 或将其委托给最近的静态元素

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

updated 更新

checkbox might have multiple values so to get all the values that is checked you need to loop through the values or use map() 复选框可能有多个值,以便获取您需要循环遍历值的所有值或使用map()

try this 尝试这个

   $('#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
   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM