gpt4 book ai didi

javascript - 当某些字段和表数据为空时停止保存

转载 作者:行者123 更新时间:2023-12-02 16:53:45 26 4
gpt4 key购买 nike

在此代码中,如果任何表格单元格为空,则不允许保存,但是,我现在希望它即使最后一个表格单元格为空也可以保存。

How can i add here for last input of table row where type is not hidden?

 $('#myTable tr input[value != add]:text').filter(function () {});

查看我的FIDDLE

这是我的 JavaScript 代码:

 $("#btnSave").click(function (event) {
var flag = false;
var emptyBoxes;
var $rows = $('#myTable tr:not(:hidden)');
$rows.each( function () {
emptyBoxes = $('#myTable tr input[value != add]:text').filter(function () {
return this.value == "";
});

if (emptyBoxes.length != 0) {
flag = true;
}
});
if (flag) {
alert("this cannot be empty");
emptyBoxes.eq(0).focus();
} else
alert("done");
});

最佳答案

尝试以下代码:

var fields = $('input[type=text]'); /* All fields */

/* This function will find empty fields */
var findEmptyFields = function() {

var n = fields.length - 1;

/* Go throw all inputs with type=text and if someone is empty return index of this element */
for (var i = 0; i < n; i++) {
if (fields.eq(i).val() === '') {
return i;
}
};

/* Else return false that means that we didn't find any empty fields*/
return false;


}

$("#btnSave").on('click', function() {

var empty = findEmptyFields();
if (empty === false) {
alert('Done');
}
else {
alert('Some field is empty');
fields.eq(empty).focus();
}

});

关于javascript - 当某些字段和表数据为空时停止保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26351494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com