jquery each() 只循环通过 asp.net gridview 一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18640074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:06:14  来源:igfitidea点击:

jquery each() only looping through asp.net gridview once

jqueryasp.netgridview

提问by derekjs67

I'm trying to loop through a gridview and throw an alert to the user if they haven't filled out the quantity on the item they have checked. But it only works on the first checkbox, and it doesn't return false because the code still executes on the server side.

我正在尝试遍历 gridview 并向用户发出警报,如果他们没有填写他们检查过的项目的数量。但它只适用于第一个复选框,并且不会返回 false,因为代码仍然在服务器端执行。

function GetCheckedRows() {
    var checkBox = $("#flexCheckBoxList")
    var textBox = $("#flexTextBox")
    $("#flexGridView tr").each(function () {
        if ($(checkBox).is(':checked')) {
            if (textBox.val().length === 0) {
                alert("You must specify the amount needed");
                return false;
            }
            else {
                return true;
            }
        }
    });
}

Completed! Wanted to show the finished code that loops through and stops when something isn't correct. I had to create a variable to stop loop execution. This not only checks for an empty value but checks if it is a number too.

完全的!想要显示完成的代码,当出现错误时循环并停止。我必须创建一个变量来停止循环执行。这不仅会检查空值,还会检查它是否是数字。

function GetCheckedRows() {
    var exitSubmit = false;
    $(".gridView tr").each(function (e) {
        var checkBox = $(this).find("input[type='checkbox']");
        var textBox = $(this).find("input[type='text']");
            if (checkBox.is(':checked')) {
                if (textBox.val().length === 0 || !$.isNumeric($(textBox).val())) {
                    exitSubmit = true;
                    return false;
            }
            else {
            return true;
            }
        }
    });
    if (exitSubmit) {
       alert("Please enter a valid amount");
       return false;
}

}

}

回答by Reinstate Monica Cellio

Change your script so that you find the checkbox and textbox on the row that you are finding with each()...

更改您的脚本,以便您在要查找的行上找到复选框和文本框each()...

function GetCheckedRows() {
    $("#flexGridView tr").each(function () {
        var $checkBox = $(this).find("input[type='checkbox']");
        var $textBox = $(this).find("input[type='text']");
        if ($checkBox.is(':checked')) {
            if ($textBox.val().length === 0) {
                alert("You must specify the amount needed");
                return false;
            }
            else {
                return true;
            }
        }
    });
}

If the checkbox and textbox have classes then it would make more sense to use them in the selectors, rather than inputs (as there may be other checkboxes and text boxes in each row - I don't know).

如果复选框和文本框有类,那么在选择器而不是输入中使用它们会更有意义(因为每行中可能还有其他复选框和文本框 - 我不知道)。

Also, your question suggests that you use the same ID for multiple checkboxes and multiple textboxes. IDs must be unique - you can't use them to select multiple elements, like you can with classes.

此外,您的问题建议您对多个复选框和多个文本框使用相同的 ID。ID 必须是唯一的 - 您不能像使用类一样使用它们来选择多个元素。

回答by juju

Use the jquery context selector to select thec check box within each row,like this:

使用 jquery 上下文选择器在每一行中选择 c 复选框,如下所示:

function GetCheckedRows() {
$("#flexGridView tr").each(function () {
var $this = $(this), $checkBox = $("#flexCheckBoxList", $this);
    if ($checkBox.is(':checked')) {
        if (textBox.val().length === 0) {
            alert("You must specify the amount needed");
            return false;
        }
        else {
            return true;
        }
    }
});

}

}

回答by Serjik

give you CheckBoxesa class like flexCheckBoxand you TextBoxesflexTextBox

给你CheckBoxes一个类喜欢flexCheckBoxTextBoxesflexTextBox

function GetCheckedRows() {
    var checkBox = null;
    var textBox = null;
    $("#flexGridView tr").each(function () {
        checkBox = $(this).find(".flexCheckBox");
        textBox = $(this).find(".flexTextBox");
        if (checkBox.is(':checked')) {
            if (textBox.val().length === 0) {
                alert("You must specify the amount needed");
                return false;
            }
            else {
                return true;
            }
        }
    });
}