使用 jquery 获取 HTML 表中特定行的直接几行的问题

发布于 2024-11-27 17:27:22 字数 2694 浏览 3 评论 0原文

我在我的 asp.net 网站中使用嵌套转发器。使用这些转发器,我以表格形式显示产品列表(表格放置在外部转发器中)。在外部(顶级)中继器中,我有主要产品,在内部中继器中,我有一些与外部中继器中的产品相关的选项(如果有的话)。下订单时,用户只能选择其中的 2 个与任何产品关联的选项。为了进行此验证,我使用带有以下客户端 js 函数的自定义验证器。

function CountChosenAdditionalOptions(source, arguments) {

        $('table.productslist tbody tr.productTextAlign').each(function () {
            var count = 0;
            $('table. productslist tbody tr.additionalOptions').each(function () {
                var chooseAdditionalOptionsCheckBoxtd = $(this).children("td.chooseOptionCheckBox");
                var additionalOptionChosen = $(chooseAdditionalOptionsCheckBoxtd).children("input[type=checkbox]")[0].checked;
                if (additionalOptionChosen) {
                    count = count + 1;
                }
            });
            if (count > 2) {
                arguments.IsValid = false;
                return false;
            }
        });       
    }

中继器呈现的示例 HTML 如下

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

验证背后的想法是迭代外部中继器,并针对其中的每个项目检查内部中继器中选择的选项数量。但是由于中继器只是简单地呈现为表格,对于每个产品行,使用如上所述的 js 函数,它会尝试查找附加选项行并递增计数,无论附加选项行是否与该特定产品相关联。

有人可以帮助更新 js 函数来识别与特定产品相关的选项并增加指示所选选项数量的计数吗?

更新:

function CountChosenAdditionalOptions(source, arguments) {

        $('table.productslist tbody tr.productTextAlign').each(function () {
            var count = 0; $(this).nextUntil("tr.productTextAlign").each(function () {
                var chooseAdditionalOptionsCheckBoxtd = $(this).children("td.chooseOptionCheckBox");
                var additionalOptionChosen = $(chooseAdditionalOptionsCheckBoxtd).children("input[type=checkbox]")[0].checked;
                if (additionalOptionChosen) {
                    count = count + 1;
                }
            });
            if (count > 2) {
                arguments.IsValid = false;
                return false;
            }
        });       
    }

I am using nested repeaters in my asp.net website.Using these repeaters I am displaying a list of products in tablular form(table is placed in the outer repeater). In the outer(top level) repeater I have the main products and in the inner repeater I have some options associated with the product in the outer repeater(if there are any).While placing an order user is allowed to choose only 2 of the options associated with any product.In order to do this validation I am using a custom validator with the following client side js function.

function CountChosenAdditionalOptions(source, arguments) {

        $('table.productslist tbody tr.productTextAlign').each(function () {
            var count = 0;
            $('table. productslist tbody tr.additionalOptions').each(function () {
                var chooseAdditionalOptionsCheckBoxtd = $(this).children("td.chooseOptionCheckBox");
                var additionalOptionChosen = $(chooseAdditionalOptionsCheckBoxtd).children("input[type=checkbox]")[0].checked;
                if (additionalOptionChosen) {
                    count = count + 1;
                }
            });
            if (count > 2) {
                arguments.IsValid = false;
                return false;
            }
        });       
    }

And the sample HTML rendered by the repeaters is as follows

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

The idea behind the validation is to iterate through the outer repeater and for each item in it check the number of options selected in the inner repeater.But as the repeaters are simply rendered as table, with js function as above for each product row it is trying to find the additional option row and incrementing the count irrespective of whether the additional option row is associated with that particular product or not.

Could some one please help in updating the js function to identify the options associated with a particular product and increment the count indicating the number of options chosen?

Update:

function CountChosenAdditionalOptions(source, arguments) {

        $('table.productslist tbody tr.productTextAlign').each(function () {
            var count = 0; $(this).nextUntil("tr.productTextAlign").each(function () {
                var chooseAdditionalOptionsCheckBoxtd = $(this).children("td.chooseOptionCheckBox");
                var additionalOptionChosen = $(chooseAdditionalOptionsCheckBoxtd).children("input[type=checkbox]")[0].checked;
                if (additionalOptionChosen) {
                    count = count + 1;
                }
            });
            if (count > 2) {
                arguments.IsValid = false;
                return false;
            }
        });       
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪冰清 2024-12-04 17:27:23

我想你想要nextUntil

http://api.jquery.com/nextUntil/

$('table.productslist tbody tr.productTextAlign').each(function () {

    $(this).nextUntil("tr.productTextAlign").each(function () {

    }
}

第二个循环将从之后开始的每一行运行当前的 productTextAlign 并继续,直到找到另一个 productTextAlign

I think you want nextUntil.

http://api.jquery.com/nextUntil/

$('table.productslist tbody tr.productTextAlign').each(function () {

    $(this).nextUntil("tr.productTextAlign").each(function () {

    }
}

The 2nd loop will run for every row starting after the current productTextAlign and continuing until it finds another productTextAlign.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文