Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery each()仅在asp.net gridview中循环一次_Jquery_Asp.net_Gridview - Fatal编程技术网

jquery each()仅在asp.net gridview中循环一次

jquery each()仅在asp.net gridview中循环一次,jquery,asp.net,gridview,Jquery,Asp.net,Gridview,我试图通过gridview循环,如果用户没有填写他们检查的项目的数量,我会向用户发出警报。但是它只在第一个复选框上起作用,并且不会返回false,因为代码仍然在服务器端执行 function GetCheckedRows() { var checkBox = $("#flexCheckBoxList") var textBox = $("#flexTextBox") $("#flexGridView tr").each(function () { if (

我试图通过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;
            }
        }
    });
}
完成了!希望显示完成的代码,在出现错误时循环并停止。我必须创建一个变量来停止循环执行。这不仅检查空值,还检查它是否也是一个数字

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

}

更改脚本,以便在使用
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;
            }
        }
    });
}
如果复选框和文本框有类,那么在选择器中使用它们比在输入中使用它们更有意义(因为每行中可能有其他复选框和文本框,我不知道)


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

为您提供
复选框
类,如
flexCheckBox
文本框
flexTextBox

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;
            }
        }
    });
}

使用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;
        }
    }
});

}

这是因为您正在使用id循环将它们作为类并尝试循环。您发布的函数不足以停止表单发布。它只返回一个true/false,除非您正确地劫持了click事件(如
),否则它不会对UI产生任何影响。请发布其余的代码,特别是GridView的ASPX。显示GridView标记会更方便,IMHO。@Graham他没有提到提交表单。在验证之后,他可以做任何事情。感谢您的快速回复!这是buttonGreat的标记!当我将输入[type='textbox']更改为text时,这解决了我的问题