先前开了一个帖子问这个问题,但当时有个牛人们帮助解答了另外一个问题,反而原帖的问题没有解决。为了对得住大牛的帮助,我就先结贴,再开新帖问。问题描述如下:从被点击了checkbox的所在行的记录取得最关键的信息,加上最上端的select下拉list中选择,并用于操作数据库,这个过程可以分为这几个步骤:
1. 生成car 1、car 2等一系列这样的记录表,这个我可以搞定;基本代码为:<!---下面只是示例,真正的记录表肯定要循环得到---->
<table cellpadding="0px" cellspacing="0px" border="0px" style="text-align:left; width:100%;">
                            <tr>
                                <td colspan="7"><h2>Car 1</h2></td>
                            </tr>
                            <tr>    
                                <th width="30px"><p>No.</p></th>
                                <th width="120px"><p>Tank No.</p></th>
                                <th width="120px"><p>Departure</p></th>
                                <th width="120px"><p>Arrival</p></th>
                                <th width="70px"><p>Work No.</p></th>
                                <th colspan="2"></th>
                            </tr>
                          <tr><td colspan="7" style="padding-top:6px;"></td></tr>
                            <tr style="height:30px; valign="middle">    
                                <td><p>1</p></td>
                                <td><p>RYCU9930357</p></td>
                                <td><p>AAAAAAA</p></td>
                                <td><p>BBBBBBB</p></td>
                                <td><p>1031239</p></td>
                                <td></td>
                                <td><input type="checkbox" class="checkbox" /></td>
                            </tr>
                            <tr><td colspan="7" style="padding-top:6px;"></td></tr>
                            <tr style="height:30px;  valign="middle">    
                                <td><p>2</p></td>
                                <td><p>RYCU9930357</p></td>
                                <td><p>AAAAAAA</p></td>
                                <td><p>BBBBBBB</p></td>
                                <td><p>1031239</p></td>
                                <td></td>
                                <td><input type="checkbox" class="checkbox" /></td>
                            </tr>
                        </table>
                       <tr style="margin-bottom:12px; margin-top:12px;">
                        <td style="background-color: #316C8C; border:1px solid  #316C8C; border-radius:5px; padding:10px; color:#ffffff;">
                                  <input type="submit" value="change" class="blue_btn">
                        </td>
                       </tr>
</table>上面没有加入select的list。最后代码肯定是要循环得到的,目标想象图为:这部分,我自己应该能完成。2.后面的步骤就是点击checkbox,取出该条记录的id,传给后台。我已经可以按照街头小贩的思路,把该记录的id用hidden的方式嵌入即可该记录。但如何把点击过的checkbox和这个嵌入的值联系起来?根据其他大牛的指点,我是不是应该在<input type="checkbox" name="这里取个动态的name?" value="这里通过循环给出该记录的id?">。这里就有点思路阻塞了。3.后台数据库操作以及返回信息,由js操作,这部分似乎应该没有问题了,我自己可以摸索解决。原帖中几个大牛说可以类似于<input type="checkbox" name=??? value=??? a1='' a2='' a3=.....>,这个怎么和所在行记录的id数值联系起来?关键存在多个记录表,且是循环给出的,读取数据时name还不能混乱?请大牛们指导啊,棘手啊!!!!(另外不要担心结贴和给分问题,不够我可另补!)

解决方案 »

  1.   

    但如何把点击过的checkbox和这个嵌入的值联系起来?根据其他大牛的指点,我是不是应该在<input type="checkbox" name="这里取个动态的name?" value="这里通过循环给出该记录的id?">。这里就有点思路阻塞了。
    re:什么意思?是根据选中的checkbox,把跟选中的checkbox的值所在行的所有数据取出来
      

  2.   


    不要求局部刷新,最简单方法传统表单提交,都放在 一个form下,
    checkbox的name 都一样,value=数据记录ID
      

  3.   

    请大牛们多多帮忙。因为小弟刚接触js\ajax\jquery,一直很头疼,所以小弟在这里恳求:如果可以的话,能否给出核心问题的关键代码片段或者指出类似网上案例,这样方便小弟揣摩。
    万分感谢啊!
      

  4.   

    我以前给别人作的一个示例。瞧瞧是否跟你的需求有点接近<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type"content="text/html; charset=gb2312"/><script type="text/javascript"src="cart.js"></script><title>Shopping Cart demo</title><style type="text/css">div{float:left;margin:0;padding:0;width:100%;height:100%}div ul{margin:5px;padding:0;width:330px;height:auto}div ul li{float:left;margin:0;padding:0;width:80px;height:30px;list-style:none}.input1{border:1px solid#ddd}div ul li{font-family:Arial,Helvetica,sans-serif;font-size:14px;color:#666}div ul.caption li{color:#f00}*label{font-family:Arial,Helvetica,sans-serif;font-size:14px;color:#333}</style></head><body><form name="form1"method="post"action="add.asp"onSubmit="return checkOrder();"><div><ul class="caption"><li>礼品</li><li>积分</li><li>数量</li><li>金额</li></ul><ul><li>礼品1<input type="hidden"name="prize"value="prize_1_name"/></li><li><input type="hidden"name="prize_1_unit"id="prize_1_unit"size="8"value="8"/>8</li><li><input type="text"name="prize_1"size="8"class="input1"onchange="prizecalculator(this);"/></li><li><span id="prize_1_balance"></span></li><script typ="text/javascript">itemarr.push("prize_1_balance");</script></ul><ul><li>礼品2<input type="hidden"name="prize"value="prize_2_name"/></li><li><input type="hidden"name="prize_2_unit"id="prize_2_unit"size="8"value="30"/>30</li><li><input type="text"name="prize_2"size="8"class="input1"onchange="prizecalculator(this);"/></li><li><span id="prize_2_balance"></span></li><script typ="text/javascript">itemarr.push("prize_2_balance");</script></ul><ul><li>礼品3<input type="hidden"name="prize"value="prize_3_name"/></li><li><input type="hidden"name="prize_3_unit"id="prize_3_unit"size="8"value="10"/>10</li><li><input type="text"name="prize_3"size="8"class="input1"onchange="prizecalculator(this);"/></li><li><span id="prize_3_balance"></span></li><script typ="text/javascript">itemarr.push("prize_3_balance");</script></ul></div><label>您的总积分:<span id="totalcredit">78</span>,已消费积分:<span id="consumecredit">0</span></label><script type="text/javascript">totalcre=document.getElementById("totalcredit").firstChild.nodeValue;</script><label><input type="submit"name="btn1"value="order"/></label></form></body></html>
    里面用的js,保存为cart.jsvar totalcre;var consumecre=0;var itemarr=new Array();function prizecalculator(strNumber){if(!strNumber.value.match(/^[0-9]*[1-9][0-9]*$/)){alert("只能输入数字");strNumber.select();return}var currunit=document.getElementById(strNumber.name+"_unit").value;var currconsume=parseInt(strNumber.value)*parseInt(currunit);document.getElementById(strNumber.name+"_balance").innerHTML=currconsume;sumconsume();if(consumecre>totalcre){alert("您的积分不足以支付您所购的礼品");strNumber.select()}}function sumconsume(){var tmptotal=0;for(var i=0;i<itemarr.length;i++){var item=document.getElementById(itemarr[i]);var itembalance=(item.hasChildNodes())?parseInt(item.firstChild.nodeValue):0;tmptotal+=itembalance}consumecre=tmptotal;document.getElementById("consumecredit").innerHTML=consumecre}function checkOrder(){if(consumecre>totalcre){alert("不可透支积分");return false}if(consumecre==0)return false;return true}
      

  5.   


    上个帖子中,那个人也没有说清楚。我的理解是,把每条记录的id值嵌入到<input name="idName" values="这里嵌入该条记录的id" type="checkbox">,然后点击这个checkbox,读取该记录的value(即id值)。但存在的问题是这个记录表是循环得到的,因此name是变动的。难道真的应该这么做?
      

  6.   

    html页表单的接受页,是asp的,很早以前的代码<%
    Dim strPrizeName
    'http响应的部分内容:prize=prize_1_name,prize_2_name,prize_3_name
    '数组中样式:array("prize_1_name","prize_2_name","prize_3_name")
    strPrizeName=Split(Request.form("prize"),",")
    for i=1 to UBound(strPrizeName)+1
            '此处可能产生前或后的空格,用Trim踢掉
            Dim currItem:currItem=Trim(strPrizeName(i-1))
            '单价
            'strPrizeName(i-1)=prize_1_name 如果i=1
            'Right(strPrizeName(i-1),4)=name
            'Replace(strPrizeName(i-1),Right(strPrizeName(i-1),4),"unit")=单价的名称
            Dim tmpUnit:tmpUnit=Replace(currItem,Right(currItem,4),"unit")
            tmpUnit=Request.Form(tmpUnit)
            '数量
            'Left(strPrizeName(i-1),7)=prize_1
            Dim tmpNumber:tmpNumber=Left(currItem,7)
            tmpNumber=Request.Form(tmpNumber)
            '消费金额
            '单价*数量
            Dim tmpBalance:tmpBalance=Int(Abs(tmpUnit))*Int(Abs(tmpNumber))
            Response.Write "礼品"&i&"-->名称:"&currItem&"单价:"&tmpUnit&"数量:"&tmpNumber&"金额:"&tmpBalance&"<br/>"
    next
    %>
      

  7.   


    如果name都一样,后面的js或者form的action的php文件怎么分辨出到底是哪个记录被点击了呢?(因为name都一样)
    关键我从来没有干过checkbox的事情,头一回,蒙了。有否那关键代码一两行?谢谢先。要刷新的,不过这个不是难事,实在不行,我给它来个全部刷新。这个可以不考虑。
      

  8.   


    如果name都一样,后面的js或者form的action的php文件怎么分辨出到底是哪个记录被点击了呢?(因为name都一样)
    关键我从来没有干过checkbox的事情,头一回,蒙了。有否那关键代码一两行?谢谢先。要刷新的,不过这个不是难事,实在不行,我给它来个全部刷新。这个可以不考虑。

    form 本身提交功能就有把选的 checkbox的value 以","拼接在一起提交
      

  9.   

    上个帖子中,那个人也没有说清楚。我的理解是,把每条记录的id值嵌入到<input name="idName" values="这里嵌入该条记录的id" type="checkbox">,然后点击这个checkbox,读取该记录的value(即id值)。但存在的问题是这个记录表是循环得到的,因此name是变动的。re:假如:用户选中了id=1的checkbox,哪么该 行的所有数据都可以拿到。像无刷删除记录.代码的示例如下<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <title>无标题文档</title>
    </head><body><!---下面只是示例,真正的记录表肯定要循环得到---->
    <table cellpadding="0px" cellspacing="0px" border="0px" style="text-align:left; width:100%;">
                                <tr>
                                    <td colspan="7"><h2>Car 1</h2></td>
                                </tr>
                                <tr>    
                                    <th width="30px"><p>No.</p></th>
                                    <th width="120px"><p>Tank No.</p></th>
                                    <th width="120px"><p>Departure</p></th>
                                    <th width="120px"><p>Arrival</p></th>
                                    <th width="70px"><p>Work No.</p></th>
                                    <th colspan="2"></th>
                                </tr>
                              <tr><td colspan="7" style="padding-top:6px;"></td></tr>
                                <tr id="row1" style="height:30px;" valign="middle">    
                                    <td><p>1</p></td>
                                    <td><p>RYCU9930357</p></td>
                                    <td><p>AAAAAAA</p></td>
                                    <td><p>BBBBBBB</p></td>
                                    <td><p>1031239</p></td>
                                    <td></td>
                                    <td><input type="checkbox" class="checkbox" name="id" value="1"/></td>
                                </tr>
                                <tr><td colspan="7" style="padding-top:6px;"></td></tr>
                                <tr id="row2" style="height:30px;"  valign="middle">    
                                    <td><p>2</p></td>
                                    <td><p>RYCU9930357</p></td>
                                    <td><p>AAAAAAA</p></td>
                                    <td><p>BBBBBBB</p></td>
                                    <td><p>1031239</p></td>
                                    <td></td>
                                    <td><input type="checkbox" class="checkbox" name="id" value="2"/></td>
                                </tr>
                            </table>
                           <tr style="margin-bottom:12px; margin-top:12px;">
                            <td style="background-color: #316C8C; border:1px solid  #316C8C; border-radius:5px; padding:10px; color:#ffffff;">
                                      <input type="submit" value="change" id="btn" class="blue_btn">
                            </td>
                           </tr>
    </table>
    <script type="text/javascript">
    jQuery(function(){
    $('#btn').click(function(){
    //找到选中了哪个checkbox
    var cID=$(':checkbox:checked').val();
    //找到对应的行删除
    $('#row'+cID).remove();
    });
    });
    </script>
    </body>
    </html>
      

  10.   

    如果name都一样,后面的js或者form的action的php文件怎么分辨出到底是哪个记录被点击了呢?(因为name都一样)
    关键我从来没有干过checkbox的事情,头一回,蒙了。有否那关键代码一两行?谢谢先。
    re:因为checkbox的name=id,它的值已经标注了记录的唯一性。提交到php如果是name=id[],提交到php是一个id数组是$_POST['id']=array(1,2,3),如果没有[],提交到php时是id=1,2,3
      

  11.   

    <input id="1" name="ddd" value="{id:'111', val:'值'}" type="checkbox" class="checkbox" />动态写入checkbox的时候给value付一个对象就好了!如上面
    然后试试输出value的值
    consoloe.log($('#1').attr('value'));
    输出结果
    {id:'111', val:'值'}
      

  12.   


    因为周末和昨天特别忙的原因,今天才过来结贴。
    非常感谢各位大牛的热情帮助,基于各位大牛给我的宝贵启示,经过我的摸索,最后终于解决这个问题。
    本人水平不高,所以就采取了一个传统的php+html混合的方式来解决了。啥都不说,先上代码,不是本人水平高,而且也算是把自己的工作成果回馈大家,希望能给大家点帮助:<?php 
    $showTime = "";
    $showTime = com_getReqParamStr("showTime");/*
    if ($showTime="")
    {
    date_default_timezone_set('Europe/Paris' );
    $showTime = date("Y-m-d");
    }*/$carTotal = getCarTotal();
    $carTotal++;$mingzi="car";
    for ($i=1;$i<$carTotal;$i++)
    {
    $mingzi .=$i;
    ?>
    <input type="hidden" name="showTime" value="<?php echo $showTime; ?>">
    <div class="TransportBooking">
       <table cellpadding="0px" cellspacing="0px" border="0px" style="text-align:left; width:100%;">
          <tr>
           <td colspan="7"><h2>Car  <?php echo $i;?></h2></td>
                                </tr>
                                <tr>    
                                    <th width="30px"><p>No.</p></th>
                                    <th width="120px"><p>Tank number:</p></th>
                                    <th width="120px"><p>Departure</p></th>
                                    <th width="120px"><p>Arrival</p></th>
                                    <th width="70px"><p>Customer</p></th>
                                    <th colspan="2"></th>
                                </tr>
    <?php  $query = "select * from transport.dingdan where orderTime='$showTime' and truck=$i and status=2 order by num"; $data = getAll($query);

    foreach($data as $each)
    {
    $id = $each['num'];<!----当中省略一部分无关checkbox的代码-------->
    ?>
    </p></td>
    <td></td>
    <td><input type="checkbox" class="checkbox" name=<?php echo $mingzi;?>[] value="<?php echo $id; ?>"/></td></tr>

    <?php 

    }
    ?>
    </table> </div>
    <?php }?>
    这样运行的结果是:比如针对Car 2,如果动态有4个记录,那么每个checkbox的名字是car1,car12,car123,car1234.我用$_POST[$mingzi]这种方式一一获取。当然,$_POST[$mingzi]的值就是我所需要的记录的id值。这就是我解决这个问题的关键所在。最后就是结贴的问题,我的考虑是这样的:说老实话,给我最大启示的是上个帖子中有人说了句用传统的方式来解决,并给出大致语句。但街头小贩依然给我了强有力的帮助和解释,并且一起探讨。可以说,我现在几乎无处不用<input type="hidden" ....>这种方式了。而且,街头小贩给了我多段代码,虽然我还没有很理解这些代码含义。
    为表示感谢,给60分,其他两位参与者也或给出代码,或解释,各给20分。不知道大家对此有疑问否,若有问题,回帖或给我私信,我会考虑。若无意见,今晚结贴。
      

  13.   

    晕,昨天完成的,现在脑袋就米糊了。
    运行的结果是:比如有5辆car,那第一辆car的checkbox的名字是car1, 第二辆是car12.。。第五辆是car12345。如果第二辆car的表格中有动态记录4个(其实多少个无所谓了,因为我要求程序只能点击两次,因此只有两个值),所以点击的两个值用$result = $_POST[$mingzi],以及$result[0],$result[1]来表示。好了,到此应该算是解释清楚了。