javascript - 如何使用 javascript 将选中的复选框值传递到下一页

标签 javascript php jquery html

我正在从数据库中检索值并将其显示在带有复选框的 html 表中。我还有按钮,当用户选中复选框时,它会传递 rowid 并重定向下一页。如果用户未选中复选框并选中该按钮不会传递 rowid,也不会重定向。

我的问题是,当检查 html 表中的第一行并按下按钮时,它的工作正常,但如果我检查 html 表中的第二行并单击按钮,它不会执行任何操作

下面是我的代码

<tbody id="fbody" class="fbody" style="width:1452px" >    
<?php    

$clientid=$_GET['clientid'];  
if($clientid!=""){      
    $sql = mysql_query("SELECT * FROM billingdatainputandexport WHERE clientid =            '$clientid'");    

    while($rows=mysql_fetch_array($sql))
    {
        if($alt == 1)
        {
            echo '<tr class="alt">';
            $alt = 0;
        }
        else
        {
            echo '<tr>';
            $alt = 1;
        }

    echo '<td  style="width:118px" class="edit clientid '.$rows["id"].'">'.$rows["clientid"].'</td>
        <td id="CPH_GridView1_clientname" style="width:164px" class="edit clientname '.$rows["id"].'">'.$rows["clientname"].'</td>      
        <td id="CPH_GridView1_billingyear" style="width:168px" class="edit  billingyear '.$rows["id"].'">'.$rows["billingyear"].'</td>
        <td id="CPH_GridView1_billingmonth " style="width:169px" class="edit billingmonth '.$rows["id"].'">'.$rows["billingmonth"].'</td>
        <td style="width:167px" class=" '.$rows["id"].'">
        <input name="nochk" value=" '.$rows["id"].'" type="submit" style="margin:0 0 0 49px;background-image: url(/image/export.png);background-repeat: no-repeat;cursor:pointer;color:#C0C0C0;" ></td>
        <td style="width:69px"><input type="checkbox" id="chk1" name="chk1" value=" '.$rows["id"].'"/></td>                                 
        </tr>';   

    }
}
?>    
</tbody>

<input type="image" name="yousendit" id="yousendit" src="/image/export.png"  style="margin:-5px 23px -28px 822px;cursor:pointer;" >

javascript

<script>    
$(document).ready(function() {      
    $("#yousendit").click(function() {      
        if(document.getElementById('chk1').checked){
                var ms = document.getElementById('chk1').value;

                $.ajax({
                   type:"post",    
                   data:"ms="+ms,
                   success:function(data) {             
                        window.location = 'billingdatainputandexport/billingdatainputandexportdetailedreport.php?ms='+ms+''
                      $.post("billingdatainputandexport/billingdatainputandexportdetailedreport.php", { "test": ms } );
                          $("#result").html(data);                          
                   }
                });         
        }
    });
});    
</script>

最佳答案

您可以更改此设置:

id="chk1"
name="chk1"

<input type="checkbox" id="chk1" name="chk1" value=" '.$rows["id"].'"/>

对此:

class="chk"
name="chk"'.$rows["id"].'

'<input type="checkbox" id="chk1" name="chk"'.$rows["id"].'" value=" '.$rows["id"].'"/>'

并更新 jQuery 代码:

$("#yousendit").click(function() {
    var $check = $('.chk:checked');

    $.ajax({  //<-------------here you havn't mentioned the url
       type:"post",
       data:$check.serialize(),
       success:function(data){

           ............

       }
    });
});

关于javascript - 如何使用 javascript 将选中的复选框值传递到下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21044224/

相关文章:

带有子对象的 Javascript 递归 JSON 数组

javascript - 带有固定顶部导航的 impress.js 不工作

PHP OOP 数据库类 - 真的需要吗?

php - 视频持续时间 FFMpeg PHP

jquery 弹跳效果破坏了列表的内联对齐

jquery - 在页面加载时动画滚动到 ID

javascript - 带有链接行的 JS 可排序表?

php - 返回并添加到数据库

javascript - 让按钮永久消失

javascript - 获取所选 slider 的对象 - JQUERY UI