简体   繁体   English

我怎样才能使它需要用户点击一个按钮,要验证的形式?

[英]How can I make it necessary for the user to click a button for the form to be validated?

I have a group of buttons, and ONE of them needs to be clicked in order to proceed to the next form: 我有一组按钮,需要单击其中一个按钮才能进入下一个表单:

<div class="btn-group" data-toggle="buttons-radio">
                        <button type="button" class="btn" data-bind="click: sportYes">Yes</button>
                        <button type="button" class="btn" data-bind="click: sportBasic">Basic</button>
                        <button type="button" class="btn" data-bind="click: sportNo">No</button>
                    </div>

I am using jquery validate, and I want to write a validate method but I can't use my typical approach, such as giving a text field a value of required, eg 我正在使用jquery validate,并且我想编写一个validate方法,但是我不能使用我的典型方法,例如为文本字段提供required的值,例如

$("#FormThatNeedsToBeValidated").validate({
    rules: {
        textinput1: {
            required: true
        },
        textinput2: {
            required: true
        }}});

Even if I knew the syntax, I don't think this would help, since not ALL buttons are required. 即使我知道语法,我也不认为这会有所帮助,因为不需要所有按钮。 Is there a way to leverage the active attribute that's rendered when one of my three buttons is clicked? 有没有办法利用单击三个按钮之一时呈现的active属性?

A <button> is not eligible for validation using this plugin. <button>不符合使用此插件进行验证的条件。

This plugin will only work on the following eight kinds of data input elements (each must have a unique name attribute), which also must be contained within a form element: 此插件仅适用于以下八种数据输入元素(每个数据输入元素必须具有唯一的name属性),它们也必须包含在form元素中:

<form>

   <!-- Textbox Input - Also including the various HTML5 input types -->
   <input type="text" name="something" />

   <!-- Password Input -->
   <input type="password" name="pw" />

   <!-- Radio Button -->
   <input type="radio" name="foo" />

   <!-- Checkbox -->
   <input type="checkbox" name="bar" />

   <!-- File Upload -->
   <input type="file" name="upload" />

   <!-- Hidden Input - 'ignore: []' must be defined in the options -->
   <input type="hidden" name="hide" />

   <!-- Select Dropdown -->
   <select name="foobar"> ... </select>

   <!-- Text Area Box -->
   <textarea name="barfoo"></textarea>

</form>

Also see the "reference" page in the docs: http://jqueryvalidation.org/reference/ 另请参阅文档中的“参考”页面: http : //jqueryvalidation.org/reference/


To achieve the desired effect, you can test & submit the form upon clicking a particular button. 为了达到理想的效果,您可以在单击特定按钮后测试并提交表单。 Use the .valid() method to test/check and submit() to submit the form. 使用.valid()方法测试/检查并submit()提交表单。

HTML : HTML

<button type="button" class="btn" id="sportYes">Yes</button>
<button type="button" class="btn" id="sportBasic">Basic</button>
<button type="button" class="btn" id="sportNo">No</button>

jQuery : jQuery的

$(document).ready(function() {

    $("#FormThatNeedsToBeValidated").validate({  // <- initialize plugin on form
        // rules & options
    });

    $('#sportYes').on('click', function() {
        // stuff to do when this button is clicked
        if ($("#FormThatNeedsToBeValidated").valid()) { // <- test validity
            // stuff to do on valid form
            $("#FormThatNeedsToBeValidated").submit();  // <- submit the valid form
        }
    });

    $('#sportBasic').on('click', function() {
        // stuff to do when this button is clicked
    });

    $('#sportNo').on('click', function() {
        // stuff to do when this button is clicked
    });

});

DEMO: http://jsfiddle.net/PgCr2/ 演示: http : //jsfiddle.net/PgCr2/

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

相关问题 单击提交按钮(如果表单已验证)时的jQuery - Jquery when click on submit button, if the form is validated 如果表单未通过验证,如何防止用户进入下一步 - How can I prevent user to go to the next step if form isn't validated 如果第一个表单未正确验证,如何隐藏表单 - How can I hide form if first form is not validated properly 单击编辑按钮时如何使表单可编辑? - How can I make the form editable when I click the edit button? 当用户单击按钮时,如何使其自动在文本字段中输入特定的关键字? - How can I make it automatically input particular keyword into text field when a user click a button? 如何防止fancybox关闭? 想要使用户单击“关闭”按钮,而不是在浏览器上的任何位置 - How can I prevent fancybox from closing? Want to make the user click the Close button instead of anywhere on the browser 验证表单输入后如何在按钮单击上调用javascript方法 - How to call javascript method on button click after the form inputs have been validated 单击按钮提交时如何禁用操作表单? - How can I disable action form when click button submit? 如何根据按钮单击验证表单字段? - How can i validate form fields based on button click? 如何允许用户在 jquery 中的按钮上仅单击 3 次? - how can I allow the user to click only 3 times on a button in jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM