duanrang3357 2013-11-06 19:59 采纳率: 0%
浏览 109
已采纳

ajax javascript复选框更改无法正常工作

I am using the following ajax javascript to pass values if checkbox is checked or not, The problem which I am facing is that it is working only for the first row from the mysql fetch aray and for the rest of the results it always sends checkboxstatus = 1 as a result, no matter if the chekbox is checkied or not. here is the code:

<script>
 $(document).ready(function(e) {
   $('input[name=status]').change(function(){
    if( $('input[name=status]').prop('checked') )
       {checkboxstatus = '1';}
       else
       {checkboxstatus = '0';}
        var idm = $(this).attr('idm');
        $.ajax({
        type: "POST",
        url: "checkboxtestbackend.php",
        data: {"checkboxstatus": checkboxstatus,
        "idm":idm 
       },
        })        
        .fail(function(jqXHR, textStatus, errorThrown){alert(jqXHR+"--"+textStatus+"--"+errorThrown);});
    });//end change
  });//end ready
</script>

Usualy all the checkgoxes are checked in the first sql fetch if I uncehck the checbox it POST's 0 as value , but for the rest of the sql fetch results it always sends "1" as a POST result no matter checked or not.

this is the code which shows the checkboxes: <?php echo "<input type='checkbox' idm='$idm' name='status'"; if($row['status'] == 1){print "checked='checked'"; }echo "/>";?>

The visualizations of the status of the checkboxes is working but the change it is not.

This is the sql query fot it:

$sql = "SELECT * FROM cursos ORDER BY id DESC";
if (!$result = mysqli_query($con,$sql))
{
    die("Error: " . mysqli_error($con));
}


<?php

while($row = mysqli_fetch_array($result))
{   
    $idm = $row['id'];
?>

<td align="center"><?php echo "<input type='checkbox' idm='$idm' name='status'"; if($row['status'] == 1){print "checked='checked'"; }echo "/>";?></td>
  • 写回答

2条回答 默认 最新

  • doulu2591 2013-11-06 20:10
    关注

    This works for me...

    I changed $('input[name=status]').prop('checked') to $(this).prop('checked').

    <input type="checkbox" idm="stuff" name="status" value="1">
    <input type="checkbox" idm="stuff" name="status" value="2">
    <input type="checkbox" idm="stuff" name="status" value="3">  
    
    <script>
     $(document).ready(function(e) {
    $('input[name=status]').change(function(){
        if( $(this).prop('checked') ) {checkboxstatus = '1';}
                                 else {checkboxstatus = '0';}
            var idm = $(this).attr('idm'); 
            $.ajax({
               type: "POST",
               url: "checkboxtestbackend.php",
               data: {"checkboxstatus": checkboxstatus, "idm":idm },
               success: function(response){
                  console.log(response);  
                 }
            })        
            .fail(function(jqXHR, textStatus, errorThrown){alert(jqXHR+"--"+textStatus+"--"+errorThrown);});
    });//end change
    });//end ready 
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab