简体   繁体   English

如何在保留默认表单警告的同时,防止提交按钮重新加载页面?

[英]How to prevent the submit button from reloading the page, while keeping default form warnings?

I have a simple form to get a table's row/column numbers, with a button type = "submit" that reloads the page onClick . 我有一个简单的表格来获取表的行/列号,其按钮type = "submit" ,可重新加载页面onClick。

I want to keep the warnings when the user enters an invalid value, since the inputs have a maximum/minimum number required, which is not possible with the return false; 我想在用户输入无效值时保留警告,因为输入具有所需的最大/最小数字,而对于return false;则不可能return false; in the end of the onClick method, or event.preventDefault(); 在onClick方法或event.preventDefault();的末尾event.preventDefault();

Any ideas how to do that? 任何想法如何做到这一点?

This code is in JSX: 这段代码在JSX中:

<form id="table_form">
   <label>how many rows ?</label>
   <input
     type="number"
     name="rows"
     min={min_tablerows}
     max={max_tablerows}
     required
     value={tablerows}
     onChange={onChangeHandeler} />
   <label>how many columns ?</label>
   <input
     type="number"
     name="columns"
     min={min_tablecolumns}
     required
     value={tablecolumns}
     onChange={onChangeHandeler} />
   <button
     type="submit"
     form="table_form"
     onClick={onClickHandeler}>
       <FontAwesomeIcon icon={faCheck} />
   </button>
</form>

You want to keep the default html warnings if I understand your question well. 如果我很了解您的问题,则希望保留默认的HTML警告。 I would like to suggest the following: 我想提出以下建议:

  1. Firstly, you don't need to attach onClick event handler for the button because a button in an HTML form or with the type submit by default submits to the form it is contained. 首先,你不需要附加onClick事件处理程序button ,因为button在HTML form或类型submit默认提交到包含它的形式。

  2. You can attach onSubmit event handler to the main form and in its callback function prevent the default behaviour. 您可以将onSubmit事件处理程序附加到主窗体,并在其回调函数中阻止默认行为。

  3. With this, html5 will handle any errors and prevent the form from even submitting in the first place. 有了这个,html5将处理所有错误,甚至阻止表单提交。 When it doesn't find any error, it will run the callback function attached to the form's event handler. 当找不到任何错误时,它将运行附加到窗体的事件处理程序的回调函数。

So your code might be as follows: 因此,您的代码可能如下所示:

handleSubmit(e) {
e.preventDefault();
// Do what you like here

e.target.submit(); // if you want to submit the forms
}

<form id="table_form" onSubmit={this.handleSumbit.bind(this)}>
   <label>how many rows ?</label>
   <input
     type="number"
     name="rows"
     min={min_tablerows}
     max={max_tablerows}
     required
     value={tablerows}
     onChange={onChangeHandeler} />
   <label>how many columns ?</label>
   <input
     type="number"
     name="columns"
     min={min_tablecolumns}
     required
     value={tablecolumns}
     onChange={onChangeHandeler} />
   <button
     type="submit">
       <FontAwesomeIcon icon={faCheck} />
   </button>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM