如果表单提交成功并保留在页面上,如何使用js重定向它的错误
How to redirect if form submission is successful and remains on the page its false using js
这是我的代码。我想在成功提交表单后重定向页面,它仍然在该页面上,用户需要修复错误
<form id="form_1" name="form" action="#" method="POST">
<div class="btn-group" data-toggle="buttons">
<input class="radio_btn" type="radio" name="group1" value="true">Yes
<input class="radio_btn" type="radio" name="group1" value="false">No
</div>
<div class="btn-group" data-toggle="buttons">
<input class="radio_btn" type="radio" name="group2" value="true">Yes
<input class="radio_btn" type="radio" name="group2" value="false">No
</div>
<button type="submit">Submit</button>
</form>
<script>
var form = document.querySelector('#form_1');
var radioList = form.querySelectorAll('.radio_btn');
form.addEventListener('submit', function() {
for (var i = 0, length1 = radioList.length; i < length1; i++) {
if (radioList[i].getAttribute('value') == 'false' && radioList[i].checked) {
alert("error");
return false;
} else {
return true;
window.location.assign("http://worldlivecricket.com");
}
}
});
</script>
你可以这样使用 jquery
$("#form_1").on("submit",function(e){
if(your validation) {
// redirect
} else {
e.preventDefault();
}
});
这里 e.preventDefault();
将停止页面重新加载
这是我的代码。我想在成功提交表单后重定向页面,它仍然在该页面上,用户需要修复错误
<form id="form_1" name="form" action="#" method="POST">
<div class="btn-group" data-toggle="buttons">
<input class="radio_btn" type="radio" name="group1" value="true">Yes
<input class="radio_btn" type="radio" name="group1" value="false">No
</div>
<div class="btn-group" data-toggle="buttons">
<input class="radio_btn" type="radio" name="group2" value="true">Yes
<input class="radio_btn" type="radio" name="group2" value="false">No
</div>
<button type="submit">Submit</button>
</form>
<script>
var form = document.querySelector('#form_1');
var radioList = form.querySelectorAll('.radio_btn');
form.addEventListener('submit', function() {
for (var i = 0, length1 = radioList.length; i < length1; i++) {
if (radioList[i].getAttribute('value') == 'false' && radioList[i].checked) {
alert("error");
return false;
} else {
return true;
window.location.assign("http://worldlivecricket.com");
}
}
});
</script>
你可以这样使用 jquery
$("#form_1").on("submit",function(e){
if(your validation) {
// redirect
} else {
e.preventDefault();
}
});
这里 e.preventDefault();
将停止页面重新加载