【问题标题】:Don't submit form if nothing selected, using jQuery ajaxForm plugin如果没有选择,不要提交表单,使用 jQuery ajaxForm 插件
【发布时间】:2009-07-21 23:08:07
【问题描述】:

我正在使用jQuery Form plugin 对我的表单进行“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>

【问题讨论】:

    标签: jquery forms callback ajaxform


    【解决方案1】:

    所有 jQuery 方法都返回一个数组,如果找不到匹配项,则返回一个空数组。如果用 'if' 检查一个 'empty' 数组,它将返回 true。

    试试

    function ischecked(formData, jqForm, options)
    {
        return $('input[name="answer_id"]:checked').length > 0; //Checking that the number of checked items are more than zero. 
    
        //You should also be able to use $('#answers input:checked'). 
    
    }
    

    编辑:作者发布 HTML 后更新了代码。

    【讨论】:

    • 当我执行 alert($('#answer:checked').length) 时,它始终为 0,即使我选择了其中一个单选框。
    • 如果您的单选按钮使用名称“myGroup”进行分组,请尝试 $('input[name="myGroup"]:checked').length
    • 是的,现在按输入名称检查会返回正确数量的“已检查”按钮。
    • 您也可以使用您的#answers 选择器。唯一的区别是您必须添加 input:checked。请参阅我的回答中的评论。
    【解决方案2】:

    试试这个:

    function ischecked(formData, jqForm, options)
    {
        return $('input[name=answer]').is('checked');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-22
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 2013-12-30
      • 2014-11-17
      • 2012-02-28
      • 2016-01-06
      相关资源
      最近更新 更多