简体   繁体   中英

When a checkbox is checked i want to send its value to other PHP file through jquery

Actually i want to send the value of checkbox when it is checked through jquery...means when i click on checkbox it has to send automatically...i tried with following code..

$(document).ready(function () {
    $(".checkBox").change(function () {
        var s = $(this).val();
        var dataString = 's=' + s;
        $.ajax({
            type: "GET",
            url: "xyz.php",
            data: dataString,
            cache: false,
            success: function (html) {}
        });
    });
});

and html code is

<div class = "checkBox">
<input type = "checkbox" name = "select[]" value = <?php $friendUid?> />
</div>

I was unable to send to other page

Change your html: Put the class = "checkBox" onto the checkbox element

<div>
<input type = "checkbox" name = "select[]" value = <?php $friendUid?>  class = "checkBox"/>
</div>

选择器$(“。checkBox”)会将该函数附加到具有checkBox类的任何项目,并且由于您的复选框没有将其作为其类,因此您实际上将函数附加到包含复选框的div,当然永远不会触发你的功能。

<?php $friendUid?>应该是<?php echo $friendUid ?>

with $(".checkbox") you're referencing something of class 'checkbox' (ie, your div, not the actual checkbox). Add the class to the <input type='checkbox'> instead

try this in your code and let us know what happens.

<script type="text/javascript">
   alert('check0');
   $(document).ready(function () {
      alert('check1');
      $(".checkBox").change(function () {
         alert('check2');
         var s = $(this).val();
         var dataString = 's=' + s;
         $.ajax({
            type: "GET",
            url: "test.php",
            data: dataString,
            cache: false,
            success: function (html) {
               alert('check3');
            }
         });
      });
   });
</script>

<input type="checkbox" class="checkBox" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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