jquery - 如何解决我的提交按钮问题,我无法移动到下一页
问题描述
实际上,我在所有这些输入字段中都使用了 required 属性,因为它们是强制性的。实际上我正在使用条件选择选项,如果用户选择是,则出现一个强制输入字段,用户必须填写,在这种情况下,我的提交按钮工作,我移至下一页但是当用户选择否并且没有强制字段出现,在这种情况下,当我单击提交按钮时,它不起作用。我该如何解决这个问题?我正在使用 jquery。
$(document).ready(function() {
Orientation();
$("#to_check_recognition").change(function() {
Orientation();
});
});
function Orientation() {
if ($("#to_check_recognition").val() == 'yes')
$("#Average_orientation").show();
else
$("#Average_orientation").hide();
}
<title> Cycle Time Calculation INSIGNUM</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<form action="form_table_output.html" name="myform" method="post" onsubmit="return ValidateForm(this);" style="width: 1200px; margin:0px auto">
<h1 title="Cycle calculation"> Cycle Time Calculation INSIGNUM </h1>
<hr><br>
<p>Cycletime calcualation of module siyes between 5mil and 15mil</p>
<table border="0" cellpadding="18" cellspacing="10" style="margin:0px 40px;">
<tr>
<td style="width: 50%">
<label for="to_check_recognition"><b>Do you have to check Orientation? <span style="color:red">*</span></b></label><br><br>
<select id="to_check_recognition" name="to_check_recognition" style="width: 350px; height: 35px; border-radius: 8px" required>
<option value="" disabled selected> Please Select... </option>
<option value="yes"> Yes</option>
<option value="no"> No </option>
</select><br><br>
</td>
<td style="width: 50%">
<div id="Average_orientation" style="display: none;">
<label for="Average_orientation"><b>Average amount of orientation check per panel <span style="color:red">*</span></b></label><br><br>
<input type="number" step="any" name="Average_orientation" style="width: 350px; height: 25px; border-radius: 8px" required /><br><br>
</td>
</tr>
<tr>
<td colspan="2" style="text-align: right; height: 225px">
<input name="submit" type="submit" value="Click To Submit" style="width: 200px; height: 50px; border-radius: 12px; color: blue; background: gold; cursor: pointer;" />
</td>
</tr>
</table>
</form>
解决方案
问题在于,尽管该字段是隐藏的,但它仍然具有required
设置的属性。因此无法提交表单。要解决此问题,您需要在显示/隐藏字段时添加/删除该属性。
另请注意,您的 JS 代码不应随意放置在table
元素的中间。将其放在<head>
或 之前</body>
。
最后,不要使用 和 等内联事件onclick
属性onsubmit
。改为使用不显眼的事件处理程序。同样,不要将您的 CSS 内联在 HTML 中。它应该从单独的样式表中应用。
说了这么多,试试这个:
$(document).ready(function() {
Orientation();
$("#to_check_recognition").change(function() {
Orientation();
});
$('form').on('submit', function(e) {
console.log('form submitting...');
if (!ValidateForm(this)) {
e.preventDefault();
}
});
});
function Orientation() {
var averageReqd = $("#to_check_recognition").val() === 'yes';
$("#Average_orientation").toggle(averageReqd);
$('input[name="Average_orientation"]').prop('required', averageReqd);
}
function ValidateForm() {
// noop
}
form {
width: 1200px;
margin: 0px auto;
}
table {
margin: 0px 40px;
}
table td {
width: 50%;
}
#to_check_recognition {
width: 350px;
height: 35px;
border-radius: 8px;
}
#Average_orientation {
display: none;
}
span,
span {
color: red;
}
#Average_orientation input {
width: 350px;
height: 25px;
border-radius: 8px;
}
td.foo {
text-align: right;
height: 225px;
}
td.foo input {
width: 200px;
height: 50px;
border-radius: 12px;
color: blue;
background: gold;
cursor: pointer;
}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<form action="form_table_output.html" name="myform" method="post">
<h1 title="Cycle calculation"> Cycle Time Calculation INSIGNUM </h1>
<hr><br>
<p>Cycletime calcualation of module siyes between 5mil and 15mil</p>
<table border="0" cellpadding="18" cellspacing="10">
<tr>
<td>
<label for="to_check_recognition">
<b>
Do you have to check Orientation?
<span>*</span>
</b>
</label><br><br>
<select id="to_check_recognition" name="to_check_recognition" required>
<option value="" disabled selected>Please Select...</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select><br><br>
</td>
<td>
<div id="Average_orientation">
<label for="Average_orientation">
<b>
Average amount of orientation check per panel
<span>*</span>
</b>
</label><br><br>
<input type="number" step="any" name="Average_orientation" required /><br><br>
</div>
</td>
</tr>
<tr>
<td colspan="2" class="foo">
<button type="submit">Click To Submit</button>
</td>
</tr>
</table>
</form>
推荐阅读
- javascript - 上传捕获图像并将其存储在文件夹中,使用用户 ID 将其文件名更改为唯一
- python - Python 总是返回 False
- python - 使用 Arduino 使用 Tkinter 时打开/关闭 LED
- r - R 计算净交易价值
- sql-server - 关于我的查询代码拥有和在哪里的问题
- go - 使用 cgo 时是否会发生数据竞争?
- scala - 如何运行示例 Scala vertx 项目
- reactjs - 为什么我得到 Erroe:期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions
- c# - SignInManager 引发无效的对象名称 AspNetUserClaims 异常
- sql - 用于循环日期和汇总数据的 SQL