gpt4 book ai didi

javascript - jQuery 按钮一次验证表单的一部分

转载 作者:可可西里 更新时间:2023-11-01 01:48:50 24 4
gpt4 key购买 nike

抱歉,如果我没有清楚地解释我的问题。

  1. 我有一个包含多个表格的表单供用户输入。
  2. 我使用 nextback 按钮来隐藏和显示不同的表格以引导用户。

现在的问题是:如何使用 next 按钮来验证当前事件的表格输入?例如,每次用户点击next时,它会检查是否所有字段都已填写?

这是一个破损的DEMO .感谢您的任何意见!

HTML

<form method="post" id="form1" action=index.html>
<table>
<H4 align="center" id="id_tab">
|<a href="#" class="Chemical"> Chemical </a>|
<a href="#" class="Crop"> Crop </a>|
<a href="#" class="Physical"> Physical </a>|
</H4>
</table><br>
<table class="tab tab_Chemical" border="0">
<tr>
<th><label for="id_wat_hl">Water Column Half life (days):</label></th>
<td><input type="text" name="wat_hl" id="id_wat_hl" /></td>
</tr>
</table>
<table class="tab tab_Crop" border="0" style="display:none">
<tr>
<th><label for="id_zero_height_ref">Zero Height Reference:</label></th>
<td><input type="text" name="zero_height_ref" id="id_zero_height_ref" /></td>
</tr>
</table>
<table class="tab tab_Physical" border="0" style="display:none">
<tr>
<th><label for="id_mas_tras_cof">Mass Transfer Coefficient (m/s):</label></th>
<td><input type="text" name="mas_tras_cof" id="id_mas_tras_cof" /></td>
</tr>
</table>
<table align="center">
<tr>
<td><input class="back" type="button" value="Back" /></td>
<td><input class="next" type="button" value="Next" /></td>
<td><input class="submit" type="submit" value="Submit" /></td>
</tr>
</table>
</form>

JS

$(document).ready(function() {
var tab_pool = ["tab_Chemical", "tab_Crop", "tab_Physical"];
var visible = $(".tab:visible").attr('class').split(" ")[1];
var curr_ind = $.inArray(visible, tab_pool);
$(".submit").hide();
$(".back").hide();

$('.next').click(function() {
if (curr_ind < 2) {
$(".tab:visible").hide();
curr_ind = curr_ind + 1;
$("." + tab_pool[curr_ind]).show();
$(".submit").hide();
$(".back").show();
}
if (curr_ind == 2) {
$(".submit").show();
$(".next").hide();
}
});

$('.back').click(function() {
if (curr_ind > 0) {
$(".tab:visible").hide();
curr_ind = curr_ind - 1;
$("." + tab_pool[curr_ind]).show();
$(".submit").hide();
$(".next").show();
}
if (curr_ind == 0) {
$(".back").hide();
}
});
$(".next").click(function() {
$(".tab tab_Chemical").validate({
rules: {
wat_hl: "required"
}
})
})
$(".next").click(function() {
$(".tab tab_Crop").validate({
rules: {
zero_height_ref: "required"
}
})
})
$(".next").click(function() {
$(".tab tab_Physical").validate({
rules: {
mas_tras_cof: "required"
}
})
})
});

最佳答案

使用表单添加验证

var validator = $('form').validate({
ignore: 'input[type="button"],input[type="submit"]',
rules: {
wat_hl: {
required: true
},
zero_height_ref: {
required : true
},
mas_tras_cof: {
required: true
}
}
});

然后在下一个处理程序中

$('.next').click(function () {
var tab = $(".tab:visible");

var valid = true;
$('input', tab).each(function(i, v){
valid = validator.element(v) && valid;
});

if(!valid){
return;
}

if (curr_ind < 2) {
$(".tab:visible").hide();
curr_ind = curr_ind + 1;
$("." + tab_pool[curr_ind]).show();
$(".submit").hide();
$(".back").show();
}
if (curr_ind == 2) {
$(".submit").show();
$(".next").hide();
}
});

演示:Fiddle

解释

  1. var valid = true:通过迭代过程保持选项卡状态的标志
  2. $('input', tab).each : 遍历当前选项卡中的每个 inputs 元素
  3. validator.element(v)验证选项卡中的每个元素
  4. valid = validator.element(v) && valid: 更新选项卡的状态

关于javascript - jQuery 按钮一次验证表单的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15126638/

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