如果没有选择任何内容,则不要提交表单,使用 jQuery ajaxForm 插件
我正在使用 jQuery 表单插件 来执行表单的“ajax”提交,其中包括大约有 4 个无线电选择。 但是,如果没有选择任何单选按钮,我希望表单不提交。
这是我到目前为止的尝试,在我在另一个 SO 答案和插件文档中找到的代码的帮助下:
$(document).ready(function() {
var options = { target: '#response',
type: 'get',
beforeSubmit: ischecked
}
$('#answer').ajaxForm(options);
});
function ischecked(formData, jqForm, options){
if ($('input:radio', $('#answer').is(':checked')))
{
return true;
}
else
{
return false;
}
}
我确信回调 ischecked
正在执行( alert()s 将触发,等等),但代码本身似乎没有检测到没有选择单选按钮,并且表单继续提交。
这是 HTML 表单:
<form action="score.php" id="answer">
<div style="margin-top: 0pt;" class="ans">
<input type="hidden" value="127" name="question_id"/>
<input type="hidden" value="f5043d5122cec6eb981c9a2fc7a0a653" name="user_id"/>
<input type="hidden" value="36" name="question_key"/>
<input type="hidden" value="37" name="next_key"/>
<ul id="answers">
<li>
<label>
<input type="radio" value="464" name="answer_id"/> mod_rewrite </label>
</li>
<li>
<label>
<input type="radio" value="465" name="answer_id"/> XML Site Map </label>
</li>
<li>
<label>
<input type="radio" value="466" name="answer_id"/> Tiny URL </label>
</li>
<li>
<label>
<input type="radio" value="467" name="answer_id"/> both a) and b) </label>
</li>
<li>
<label>
<input type="radio" value="468" name="answer_id"/> a), b) and c) can all be used to make it easier for Google to index your dynamically generated URLs. </label>
</li>
</ul>
</div>
<input type="submit" value="Submit Answer and Get Result" id="answer_submission"/>
</form>
I'm using the jQuery Form plugin to do an 'ajax' submit of my form, which consists of about 4 radio selects. However, I want the form to not submit if none of the radio buttons have been selected.
Here's my attempt so far, helped by so code I've found in another SO answer and the plugin docs:
$(document).ready(function() {
var options = { target: '#response',
type: 'get',
beforeSubmit: ischecked
}
$('#answer').ajaxForm(options);
});
function ischecked(formData, jqForm, options){
if ($('input:radio', $('#answer').is(':checked')))
{
return true;
}
else
{
return false;
}
}
I'm sure that the callback ischecked
is getting executed ( alert()
s will fire, etc ), but the code itself does not appear to be detecting that no radio buttons are selected, and the form continues to be submitted.
Here's the HTML form:
<form action="score.php" id="answer">
<div style="margin-top: 0pt;" class="ans">
<input type="hidden" value="127" name="question_id"/>
<input type="hidden" value="f5043d5122cec6eb981c9a2fc7a0a653" name="user_id"/>
<input type="hidden" value="36" name="question_key"/>
<input type="hidden" value="37" name="next_key"/>
<ul id="answers">
<li>
<label>
<input type="radio" value="464" name="answer_id"/> mod_rewrite </label>
</li>
<li>
<label>
<input type="radio" value="465" name="answer_id"/> XML Site Map </label>
</li>
<li>
<label>
<input type="radio" value="466" name="answer_id"/> Tiny URL </label>
</li>
<li>
<label>
<input type="radio" value="467" name="answer_id"/> both a) and b) </label>
</li>
<li>
<label>
<input type="radio" value="468" name="answer_id"/> a), b) and c) can all be used to make it easier for Google to index your dynamically generated URLs. </label>
</li>
</ul>
</div>
<input type="submit" value="Submit Answer and Get Result" id="answer_submission"/>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有 jQuery 方法都返回一个数组,如果找不到匹配项,则会返回一个空数组。 如果使用“if”检查“空”数组,它将返回 true。
尝试
编辑:作者发布 HTML 后更新了代码。
All jQuery methods return an array, if it can not find a match, it will return an empty array. If you check an 'empty' array with 'if', it will return true.
Try
EDIT: Updated the code after author posted HTML.
尝试这个:
Try this: