将复选框值传递给“编辑”(使用href) [英] Pass checkbox value to Edit (using href)

查看:68
本文介绍了将复选框值传递给“编辑”(使用href)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用href将选定复选框的值转移到下一页。我无法使用提交按钮。我希望使用JavaScript来完成此操作。

I'm trying to get the value of the selected checkbox to be transfered to the next page using href. I'm not in a position to use submit buttons.I want this to be done using JavaScript.

我的复选框中填充了表格行docid中的值。这是我在view.php中Checkbox的代码:

My checkbox is populated with value from a table row-docid. Here is my code for Checkbox in view.php:

... mysql_connect("$host", "$dbuser", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $doctbl_name";
$result=mysql_query($sql);
if(!$result ){ die('Could not get data: ' . mysql_error()); }
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {  ?>
<tr><td><input type="checkbox"  name="chk_docid[]" id="<?php echo $row['docid'];?>" 
         value="<?php echo $row['docid'];?>"></td> ...

在view.php顶部,我有一个EDIT Link作为菜单。

I have an EDIT Link as a menu in the top in view.php.

<a href="editdoc.php>Document</a>

我的问题:单击编辑链接时如何传递已选中复选框的值。

注意:我搜索了一个类似的问题,但是找不到。如果我错过了任何类似的问题,请向我提供链接。

Note :I searched for a similar question, but could not find one. If I missed any similar question please provide me with the link.

预先感谢
Lakshmi

Thanks in advance. Lakshmi

注意:将复选框的ID从chk_docid更改为动态行值($ row ['docid'])为

Note: changed the id of the checkbox from chk_docid to the dynamic row value ($row['docid']) as suggested by Jaak Kütt.

推荐答案

请确保您输入的ID具有不同的ID。.

make sure your inputs have different id-s..

while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {  ?>
...<input type="checkbox"  name="chk_docid[]" class="chk_input" 
id="chk_docid<?php echo $row['docid'];?>" value="<?php echo $row['docid'];?>">...

使用jQuery:

<a href="editdoc.php" id="editdoc">Document</a>

$("#editdoc").click(function(){
    var selection=$("input.chk_input:checked");
    if(selection.length){
        var href=$(this).attr('href')+'?'+selection.serialize();
        $(this).attr('href',href);
    }
    return true;
});

非jQuery:

<a onclick="submitWithChecked(this,'chk_input')" href="editdoc.php">Document</a>

function submitWithChecked(e,className){
    // fetch all input elements, styled for older browsers
    var elems=document.getElementsByTagName('input');                    
    for (var i = 0; i < elems.length; ++i) {
        // for each input look at only the ones with the class you are intrested in
        if((elems[i].getAttribute('class') === className || elems[i].getAttribute('className') === className) && elems[i].checked){
            // check if you need to add ? or & infront of a query part
            e.href+=(!i && e.href.indexOf('?')<0)?'?':'&';
            // append elements name and value to the query  
            e.href+=elems[i].name+'='+encodeURIComponent(elems[i].value);
        }
    }    
    return true;
}

在editdoc.php中,使用$ _GET ['name_of_input_element' ]

in editdoc.php fetch the values with php using $_GET['name_of_input_element']

这篇关于将复选框值传递给“编辑”(使用href)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆