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