简体   繁体   English

我想通过输入类型按钮检查表单验证,而不是通过提交,可以在这有任何帮助吗?

[英]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.

相关问题 如果按下提交按钮以检查验证表单,则输入字段被阻止 - Input field blocked if Submit button pressed to check validation form 我有一个输入类型为提交的按钮,想要使用MVC jQuery验证表单 - I have a button having input type submit want to validate the form using MVC jquery 使用“共享”按钮(而不是“提交”类型的输入元素)时,如何使表单验证起作用? - How can I get Form Validation to work when using a “shared” button (instead of an input element of type “submit”)? 我希望锚点的行为类似于输入类型提交按钮 - i want a anchor should act like and input type submit button 在jquery验证插件中提交表单之前,如何检查输入类型=“ file”的验证? - How to check validation for input type=“file” before submit the form in jquery validation plugin? 验证表单中单击提交按钮上的输入 - Validation of input on click submit button in a form 我想更改提交类型按钮的值吗? - i want to change value of a submit type button? 我希望“提交”按钮检查用户输入并向用户显示“正确”或“错误” - I want the "submit" button to check users input and display "right" or "wrong" to the user 为什么文本框不接受任何输入,我想对表单上的所有formData提交操作? - Why textbox is not taking any input, I want all formData on form submit action? 之间的任何区别 <input type=“submit”> 按钮和javascripts .submit() - Any difference between an <input type=“submit”> button and and javascripts .submit()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM