除非选中某个单选按钮,否则不要提交 [英] Don't submit unless a certain radio button is checked

查看:154
本文介绍了除非选中某个单选按钮,否则不要提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使Jquery变得简单,只想在单选按钮值为"1"时提交表单.如果没有,请不要提交表单并更改我在下面的代码中显示的某些内容.

I am trying to make a Jquery simple function and just want to submit a form only when the radio button value is "1". If not don't submit the form and change some things as I am showing in the code below.

如果单选按钮的值为"1"或"0",则表单仍在提交.

At the moment if the value of the radio button is "1" or "0" the form is submitting anyway.

<script>
$(document).ready(function() {
$('.result').hide();
$('input.prettycheckbox').prettyCheckable({});

$('input.submit').on('click', function(e) {
    e.preventDefault();
    $('input.prettycheckbox').each(function(){
        if ($(this).is(':checked') && $(this).is('input[value="0"]')) {
            $(this).attr("disabled", true);;
            $('.result').show();
            $('.advise').hide();
            $(this).parent().parent().addClass('incorrect');
        } else {
                $('form').submit();
            };
        });
    });
});
</script>

非常感谢!

推荐答案

为简化起见;覆盖表单提交事件,并且仅在单选值不为1时阻止.

To simplify; override the form submit event and only prevent if the radio value is not 1.

HTML:

<form method="post" target="">
    <input type="radio" name="mandatory" value="0" />Selecting me won't work<br />
    <input type="radio" name="mandatory" value="1" />You must select me<br />
    <input type="submit" class="submit" value="Submit" />
<form>

jQuery:

$(document).ready(function () {

    $('form').on('submit', function (e) {
        if( $('input[name=mandatory]:checked').val()!= 1)
        {
            e.preventDefault();
        }            
    });

});

请参见小提琴

这篇关于除非选中某个单选按钮,否则不要提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆