下面是我的 jquery 代码,如果 3 对用户单击复选框并且未在文本框字段中设置最小或最大级别,则不应提交表单。例如,如果选择了 MinLevelA 和 MaxLevelA 值中的任何一个,或者意味着如果选择了任何一对值,则应提交表单。仅验证。
$('#ParLevelSettings').on("click", ".Savebtn", function (event) { //on click
var isValid = true;
$('#ParLevelSettings input.isincludechk:checked, #ParLevelSettings input.ischanged[value="true"]').closest('div.row-fluid').find('.minlvl').each(function () {
var min = $(this).get(0);
var max = $(this).closest('div').next('div').find('.maxlvl').get(0)
var dataVal = $(this).data("ichange");
var minval = parseInt(min.value);
var maxval = parseInt(max.value);
if(minval != 0 && maxval != 0)
{
var isSet = $("#isSet_" + dataVal);
isSet.val(true);
return;
}
if (isNaN(minval) || minval > maxval) {
$(min).css({
"border": "1px solid red",
"background": "#FFCECE"
});
$(min).attr('title', 'Min level should not be empty or Min level should be less than the Max level or must give some value to min level');
isValid = false;
}
else {
$(min).css({
"border": "",
"background": ""
});
}
if (isNaN(maxval) || maxval < minval) {
$(max).css({
"border": "1px solid red",
"background": "#FFCECE"
});
$(max).attr('title', 'Max level should not be empty or Max level should be greater than the Min level or must give some value to min level');
isValid = false;
}
else {
$(max).css({
"border": "",
"background": ""
});
}
});
if (isValid == false) event.preventDefault();
});
最佳答案
$("#formID").on("submit", function(){
//use the condition in your example
if(condition==true){
return true;
}
else{
//display some reason why the form was not submitted.
return false;
}
});
关于javascript - 如果所有值为 0,则不应提交表单 如果从 3 对中选择一对,然后提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28370406/