简体   繁体   English

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

[英]How pass the checked checkbox value to the next page using javascript

I am retrieving value from database and showing it in html table with check boxes.i am also having button, when user check the check box and it pass the rowid and redirect next page .if user not check the check box and check the button it will not pass the rowid and it will not redirect. 我正在从数据库中检索值并在带有复选框的html表中显示它。我也有按钮,当用户选中复选框并通过rowid并重定向下一页。如果用户没有选中复选框并检查按钮它不会传递rowid,也不会重定向。

my problem is when first row in html table is checked and button pressed its working but if i am checking the second row in html table and click the button it not performing any action 我的问题是当html表中的第一行被选中并且按钮按下它的工作但如果我正在检查html表中的第二行并单击按钮它不执行任何操作

below is my code 下面是我的代码

<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 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>

You can change this: 你可以改变这个:

id="chk1"
name="chk1"

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

to this: 对此:

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

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

and update the jQuery code: 并更新jQuery代码:

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

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

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

       }
    });
});

HTML page should have only 1 element with specified ID, in your case as code is running in loop and you are assigning id HTML页面应该只有1个具有指定ID的元素,在您的情况下,代码在循环中运行并且您正在分配ID

<td style="width:69px"><input type="checkbox" id="chk1" name="chk1" value=" '.$rows["id"].'"/></td>

all the checkbox will have the same ID. 所有复选框都具有相同的ID。 Now once you try to get checked status by selector 现在,一旦您尝试通过选择器获取检查状态

if(document.getElementById('chk1').checked){

it will return true if only first checkbox is selected and ignore all the other instances. 如果仅选中第一个复选框,则它将返回true,并忽略所有其他实例。

You should use class selector and loop through elements to get comma-separated values and make ajax call. 您应该使用类选择器和循环元素来获取逗号分隔值并进行ajax调用。

First change id to class in HTML <input type="checkbox" class="chk1" name="chk1" value=" '.$rows["id"].'"/> 首先在HTML中将id更改为类<input type="checkbox" class="chk1" name="chk1" value=" '.$rows["id"].'"/>

and change JavaScript to process selected checkbox array 并更改JavaScript以处理选定的复选框数组

$(document).ready(function () {

$("#yousendit").click(function () {

    var id_list = [];

    $.each($(".chk1:checked"), function (index, element) {
        var tmpVal = $(element).val();
        id_list.push(tmpVal);
    });


    if (id_list.length > 0) {


        var ms = id_list.join();




        $.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);

            }
        });

    }
});

});

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

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