Ammar Hayder Khan
Ammar Hayder Khan

Reputation: 1335

Working with checkbox which is creating by Ajax

I am working with ajax project. 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 .

so on select the checkbox i want to do some further activity.

my html code is

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

and jQuery code is

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

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

and ajax user php is

$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

Upvotes: 0

Views: 820

Answers (2)

bipen
bipen

Reputation: 36531

you need to delegate the click event using on for dynamically added element which is checkbox in your case

 $(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()

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
   });

Upvotes: 0

Adil Shaikh
Adil Shaikh

Reputation: 44740

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 -

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

Upvotes: 2

Related Questions