代码之家  ›  专栏  ›  技术社区  ›  Ian

如果未选择任何内容,则不提交表单,使用jquery ajaxform插件

  •  0
  • Ian  · 技术社区  · 17 年前

    我正在使用 jQuery Form plugin 要对我的表单进行“Ajax”提交,它包含大约4个单选选项。但是,如果没有选择任何单选按钮,我希望表单不提交。

    这是我迄今为止的尝试,在另一个so-answer和插件文档中找到的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>
    
    2 回复  |  直到 17 年前
        1
  •  2
  •   SolutionYogi Eric Lippert    17 年前

    所有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后更新代码。

        2
  •  2
  •   Francisco    17 年前

    试试这个:

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