【问题标题】:Accessing a row based on a checkbox selected in one its cells基于在其一个单元格中选择的复选框访问一行
【发布时间】:2011-10-22 12:17:38
【问题描述】:

在我的 asp.net 网站中,我使用嵌套中继器。外部中继器用于显示产品列表,内部中继器用于显示产品附带的附加选项。 这些中继器呈现如下

<table class= “productslist”>
<tbody>
<tr>....</tr>
<tr>....</tr>
<tr class=”productTextAlign”>  ......</tr>
<tr class=”additionalOptions”> ..... </tr>
<tr class=”additionalOptions”>.....</tr>

<tr class=”additionalOptions”>.....</tr>

<tr class=”additionalOptions”>.....</tr>

<tr class=”additionalOptions”>.....</tr>
<tr>...</tr>

<tr class=”productTextAlign”></tr>

<tr class=”additionalOptions”>.....</tr>

<tr class=”additionalOptions”>.....</tr>

<tr class=”additionalOptions”>.....</tr>

<tr class=”additionalOptions”>.....</tr>
</tbody>
</table>

在上面的 HTML 中,每个带有 'productTextAlign' 类的 tr 都有一个复选框和文本框,它们的呈现方式如下

<td>
<input type=”checkbox” id =”ProductRepeater_productCheckBox_0”......>
</td>
<td class=”numberRequired”>
<input type=”text” id=”ProductRepeater_numberRequired_0”......>
</td>

每个带有 'additionalOptions' 类的 tr 都有一个与此类似的复选框。

<input type=”checkbox” id =”OptionsRepeater_optionCheckBox_0” onclick=”ConfirmProductChosen(this)"......>

单击此复选框时,我想检查关联的产品复选框(在类 'ProductTextAlign' 的 tr 中)是否被选中,如果没有,我想使用 jquery/javascript 函数发出警报.

但我不确定如何访问选中复选框的行,进而访问该行上方某处的行中的复选框(带有“.ProductTextAlign”)。

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: jquery html asp.net html-table row


    【解决方案1】:
    // bind the click event to checkboxes within .additionalOptions
    $(".additionalOptions input:checkbox").click(function() {
    
        // find the closest .additionalOptions tr and then find the closest previous sibling
        // with the .productTextAlign class, then find the checkbox within
        var $checkbox = $(this).closest(".additionalOptions").prevAll(".productTextAlign").find("input:checkbox");
    
        // if the checkbox is checked, fire the alert
        if (!$checkbox.is(":checked"))
            alert("Totally checked");
    }
    

    【讨论】:

      【解决方案2】:

      试试这个

      function ConfirmProductChosen(chkBox){
      
         var productTr = $(chkBox).closest("tr").prevAll(".productTextAlign");
      
         if(productTr.find("input[*=productCheckBox]:checked").length){
           alert("It is checked");
         }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2012-06-09
        • 1970-01-01
        • 1970-01-01
        • 2015-08-16
        • 1970-01-01
        • 2021-12-24
        • 1970-01-01
        • 1970-01-01
        • 2018-02-01
        相关资源
        最近更新 更多