2011-03-05 55 views
0

我有一个嵌套的gridview有45个复选框。我希望用户能够点击一个复选框并将其值保存在单独的gridview中,直到他们希望对其执行操作(类似于购物车)。从嵌套gridview获取复选框的值

复选框嵌套在中继器后面的第二级gridview中。

 <repeater> 
     <gridview> 
      <gridview> 
       checkbox 
      <gridview/> 
     <girdview /> 
    <repeater /> 

我有一个时间赫克试图让深,想了解jQuery的复选框的值,并认为这是一个好时机。

我在想用户会点击复选框,jQuery会得到控件的id(和值),然后我可以将它传递给ajax回发触发器并填充'shopping cart'gridview。我需要通过ajax的原因是我需要根据用户选择的复选框从数据库中获取更多值。

我想我可以从那里拿走它。现在我最大的问题是搞清楚如何从复选框中获取id和值。

+1

http://jsfiddle.net/AKU4W/只要遍历无关紧要,只要绑定到特定的框。 - 在最坏的情况下,你可以使用ASP的'$ find()'来查找复选框控件,然后绑定到它们(或者找到gridview并找到其中的所有复选框)。 – 2011-03-05 23:41:13

+0

我很想缩小这回,因为我需要了解发生了什么之前,我有解决方案...我可以看到jQuery代码点击复选框,并返回嵌套复选框之一的控件ID? – IMAbev 2011-03-06 02:28:53

+0

谢谢,布拉德。在审查了您提供的链接之后,我能够获得目前所需的位置。我现在可以点击复选框并获取值。现在我需要计算如何将这个值传递给一个gridview ... – IMAbev 2011-03-06 03:09:29

回答

1

你应该能够得到复选框的值(或数据)点击多数民众赞成的东西,如

$(“#GridView控件的包装器复选框”)。住(“点击”,功能(E) { //用价值形式点击做某事

});

您可能想要使用这些信息来填充页面上的其他位置,或者您可能希望将该值存储在数据数组中。

数据数组基本上是一种让您将关键值对数据存储在jQuery中供用户在页面上采取其他操作时使用的方式。

在这里阅读更多 - >http://api.jquery.com/data/

1

从来没有使用中继器,但也有一些简单的jQuery和HTML我认为你可以得到相同的效果,而不在GridView控件的消肿:

保存为HTML文件为例

<html> 
<head> 
    <title>Test</title> 
</head> 
<body> 
    <table id="tblItems"> 
     <tbody> 
      <tr> 
       <td>+</td><td>Category 1</td> 
      </tr> 
      <tr> 
       <td> 
        <table> 
         <tbody> 
          <tr> 
           <td><input type="checkbox" /></td><td>Item 1</td><td>Price</td> 
          </tr> 
          <tr> 
           <td><input type="checkbox" /></td><td>Item 2</td><td>Price</td> 
          </tr> 
          <tr> 
           <td><input type="checkbox" /></td><td>Item 3</td><td>Price</td> 
          </tr> 
         </tbody> 
        </table> 
       </td> 
      </tr> 
      <tr> 
       <td>+</td><td>Category 2</td> 
      </tr> 
      <tr> 
       <td> 
        <table> 
         <tbody> 
          <tr> 
           <td><input type="checkbox" /></td><td>Item 4</td><td>Price</td> 
          </tr> 
          <tr> 
           <td><input type="checkbox" /></td><td>Item 5</td><td>Price</td> 
          </tr> 
          <tr> 
           <td><input type="checkbox" /></td><td>Item 6</td><td>Price</td> 
          </tr> 
         </tbody> 
        </table> 
       </td> 
      </tr> 
     </tbody> 
    </table> 
    <table id="tblResults"> 
     <thead> 
      <tr style="font-weight:bold"> 
       <td >Item Name</td><td>Price</td> 
      </tr> 
     </thead> 
     <tbody> 

     </tbody> 
    </table> 
</body> 
</html> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" ></script> 
<script> 
    $(document).ready(function() { 
     $('#tblItems > tbody > tr').each(function(i){ 
      $row = $(this); 
      if(i % 2 == 0){//even rows are expanders 
       $row.children('td:eq(0)').click(function(){ 
        var $tmp = $(this); 
        var next = i + 1; 
        $row.parent().children("tr:eq(" + next + ")").toggle(); //toggle the next row 
        if($tmp.html() == '+'){ 
         $tmp.html('-'); 
        }else{ 
         $tmp.html('+'); 
        } 
       }); 
      }else{//odd rows are collapsed by default 
       $row.toggle(); 
      } 
     }); 

     $('input[type="checkbox"]').change(function(){ 
      var $chkBox = $(this), 
       data = $chkBox.data('checkboxData'); 

      if(!data){ //Add some data to remember to this checkbox 
       $chkBox.data('checkboxData', { 
        id: Example.GetUniqueId() // create a unique ID for this checkbox 
       }); 
       data = $chkBox.data('checkboxData'); 
      } 
      if($chkBox.is(':checked')){ //checkbox is checked 
       $("#tblResults tbody").append($chkBox.closest('tr').clone().attr('id', data.id)); //copy the checked row and add ID 
       $("#tblResults input").parent().remove(); //remove the checkbox from the copied row 
      }else{ 
       $("#" + data.id).remove(); //remove copied row when not checked 
      } 
     }); 
    }); 

    Example = {}; 

    Example.GetUniqueId = function() 
    { 
     var dateObject = new Date(); 
     var uniqueId = 
       dateObject.getFullYear() + '' + 
       dateObject.getMonth() + '' + 
       dateObject.getDate() + '' + 
       dateObject.getTime(); 

     return uniqueId; 
    }; 
</script> 
+0

我同意bloat是gridview控件,但我还没有。我已经创建了页面,不幸的是,它的嵌套量远远超过了我的需求,但它仍然有效。感谢代码 - 大量的思考。 – IMAbev 2011-03-06 02:25:41