简体   繁体   English

带两个子按钮的必填字段,格式相同

[英]required field with two submnit button in same form

I set a required field for all the fields in form and having two submit button. 我为表单中的所有字段设置了必填字段,并有两个提交按钮。 and want the required data on only for one submitonly. 并且只需要一次提交一次所需的数据。 In another submit i dont want required with form fields how to prevent the requied field in another submit button. 在另一个提交中,我不想使用表单字段来防止在另一个提交按钮中填写必填字段。

<form action="data.php">
name<input type="text" name="name" required>
std<input type="text" name="name" required>
class<input type="text" name="name" required>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="clear">
<input type="save" name="save" value="save">
</form>

in this given code the required field shoud alert only when i click on submit .and do not alert on click of save is it posible any way 在此给定的代码中,仅当我单击Submit时,才应提醒必需的字段,并且在单击Save时不发出警告,是否有可能

You may consider catching submit events and doing different checks according to what button is pressed. 您可以考虑捕获提交事件并根据所按下的按钮进行不同的检查。 Also please note that the required attribute will always make your input truly required. 另外请注意,required属性将始终使您的输入真正成为必需。 Here I used a custom attribute, that has no special property, but I would then use it to identify in my custom code what fields are required. 在这里,我使用了一个没有特殊属性的自定义属性,但是随后我将使用它来在我的自定义代码中标识哪些字段是必需的。

Here is how it would look like in Javascript using jQuery: 这是使用jQuery的Javascript的外观:

<form id="your_form" action="data.php">
name<input type="text" name="name" is_required>
std<input type="text" name="name">
class<input type="text" name="name" is_required>
    <br />
<input type="submit" name="submit2" value="submit">
</form>
<input type="submit" name="submit" value="submit with check">

var ok;
$('[name=submit]').on('click', function(e) {
    ok = true;
    $('[is_required]').each(function() {
       if (!$(this).val()) ok = false;
    });
    if (!ok) {
        alert("Please fill in all the required fields");
    } else
        $("#your_form").submit();
});

You may see this code in action here: http://jsfiddle.net/z9dkjdtz/ 您可能会在这里看到此代码的实际作用: http : //jsfiddle.net/z9dkjdtz/

Use the formnovalidate attribute in a button to specify that normal HTML5 form validation (such as checking that all required fields have value) be suppressed: 在按钮中使用formnovalidate属性可指定formnovalidate常规HTML5表单验证(例如检查所有required字段是否具有值):

<input type="submit" name="save" value="save" formnovalidate>

Note: type="save" is invalid and gets ignored, so the the element would create a text input box (since type="text" is the default). 注意: type="save"无效且被忽略,因此该元素将创建一个文本输入框(因为type="text"是默认值)。

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

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