[英]i want to check form validation by input type button, not by submit, could any one help in this?
i want to only validate the form and don't want to submit it, so that i can use the form values in modifying other part of the same html page by calling a function "myfunction()" after form validation. 我想只验证表单,不想提交它,以便我可以通过在表单验证后调用函数“myfunction()”来修改表单值来修改同一个html页面的其他部分。 for this i want to use a button suggest me required code.my code is following :- 为此,我想使用一个按钮,建议我需要code.my代码如下: -
<form name="form1">
<input type="text" name="name1" required></input>
<button onclick="myfunction()" ></button> // i want to validation of form by this button
</form>
You can try this by setting onsubmit
event of form to return false;
你可以通过设置onsubmit
事件的形式来return false;
来尝试这个return false;
as follows: 如下:
<form name="form1" onsubmit="return false;">
<input type="text" name="name1" required></input>
<button onclick="myfunction();" ></button>
</form>
This will not submit the form on clicking the button but will execute myfunction()
instead. 这不会在单击按钮时提交表单,而是执行myfunction()
。
If you know jQuery, then you can do this as follows: 如果您了解jQuery,那么您可以按如下方式执行此操作:
$('form[name="form1"]').submit(function (event) {
// This will prevent form being submitted.
event.preventDefault();
// Call your function.
myfunction();
});
For maintainability consider adding an event listener to the button by selection instead of inline. 为了可维护性,请考虑通过选择而不是内联向按钮添加事件侦听器。 When the button is clicked an event object is passed to the callback. 单击该按钮时,会将事件对象传递给回调。 Event objects have a number of properties and methods. 事件对象具有许多属性和方法。 In this case you're looking for the method "preventDefault" which prevents the default action of the event which in this case is a form submit. 在这种情况下,您正在寻找方法“preventDefault”,它阻止事件的默认操作,在本例中是表单提交。 An example: 一个例子:
<form name="form1">
<input type="text" name="name1" required />
<button id="my-button"></button>
</form>
document.getElementById('my-button').addEventListener('click', function(e){
e.preventDefault();
var form = document.forms['form1']; //or this.parentNode
//do stuff
}, false);
i have achived this goal by modifying code as follow:- 我通过修改代码实现了这个目标如下: -
<form name="form1" onsubmit="myfunction();return false;">
<input type="text" name="name1" required></input>
<button >check form and call function</button>
</form>
by this i am able to check form and call my function and form is also not submitted in this case. 通过这个我能够检查表格并调用我的功能和表格在这种情况下也没有提交。 now i want to reset the form without clicking any button. 现在我想重置表单而不点击任何按钮。 suggest javascript code for this. 为此建议javascript代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.