jquery验证插件远程检查单选按钮值

时间:2011-07-03 05:43:03

标签: jquery validation jquery-plugins jquery-validate radio-button

我已将远程验证规则添加到一组单选按钮。

我希望服务器端脚本测试单选按钮的值('y'或'n'),并根据无线电选择返回不同的值。

我遇到的问题是无论选择哪个单选按钮,发送到服务器的值总是'y'

我只返回POST数据以确保

这是有问题的代码

HTML

<form id="signup-form">

    <div>
        <label for="mbr_org_name">Org. Name*</label>
        <input type="text" name="mbr_org_name" id="mbr_org_name" class="required" />
    </div>

    <div>
        <label for="mbr_tl_account_number">Account Number</label>
        <input type="text" name="mbr_tl_account_number" id="mbr_tl_account_number" class="digits" value="" />
    </div>

    <p class="field-note" id="mbr_credit_note">Have you previously established credit terms with Terri Lynn?</p>
    <div class="radio" id="mbr_credit_wrapper">
        <label for="mbr_credit_y">Yes, I have.</label>&nbsp;<input type="radio" value="y" id="mbr_credit_y" name="mbr_credit" class="ignore" disabled="disabled" /> <br />
        <label for="mbr_credit_n">No, I havn&rsquo;t.</label>&nbsp;&nbsp;<input type="radio" value="n" id="mbr_credit_n" name="mbr_credit" class="ignore" disabled="disabled" />
    </div>
    </form>

JS

$('#signup-form').validate({

            // enabled for developments
            debug: true,

            // special validation rules that could not be included as <input /> classes
            rules: {
                // account numbers are all 6 digits
                mbr_tl_account_number: {
                    minlength: 6,
                    maxlength: 6
                },
                // if the account number field has something in it
                // then the user must specify if they have credit, y or n
                // we will perform a validation against account numbers and org names to make sure this is the case
                mbr_credit: {
                    required: '#mbr_tl_account_number:filled',
                    remote: {
                        url: '/tlfr_account_check.php',
                        //cache: false,
                        type: 'post',
                        data: {
                            mbr_org_name: function() {
                                return $('#mbr_org_name').val();
                            },
                            mbr_tl_account_number: function() {
                                return $('#mbr_tl_account_number').val();
                            }
                        }
                    }
                },
                password_confirm: {
                  equalTo: '#password'
                }
            },

            // special error messages if the fail the credit check
            messages: {
                mbr_credit: {
                    required: "Please choose one."
                }
            }

        });

        $('#mbr_tl_account_number').focusout(function(evt){
            $(this).removeData("previousValue");
            $("#mbr_credit_wrapper input").removeData("previousValue");



            if($(this).valid() === 1){
                $('#mbr_credit_note').fadeIn('3000');
                $('#mbr_credit_wrapper').fadeIn('3000');
                $('#mbr_credit_wrapper input').removeClass('ignore').removeAttr('disabled');
            }

        });

PHP

<?

// make sure this request is coming from ajax
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{

$post_data = $_POST['mbr_credit'].'||'.$_POST['mbr_org_name'].'||'.$_POST['mbr_tl_account_number'];
echo '"post '.$post_data.' data"';

}
else{
die();
}
?>

1 个答案:

答案 0 :(得分:0)

最终我放弃了单选按钮,但我能够通过定位父元素来传递正确的检查值...做这样的事情

$('#signup-form').validate({
     rules: {
        mbr_credit: {
            remote: {
                url: '/tlfr_account_check.php',
                data: {
                    mbr_credit_checked:function() {
                        return $('div#radio_button_wrapper input:checked').val();
                    }
                } 
            }
        }
    });