0

I'm trying to get all the radio buttons with their names and check if they are blank. If one is blank, then the form will show an alert and will not submit. If all buttons are filled, then it will go to the "else" part and see whether the answer is correct. If it is correct, then it will show the right values in that page <div>. I have to go through the reference of  How to check a radio button with jQuery ?, but it seems it can't help me at all.

Here's my code:

uc_apply_flag keep returning 0
var uc_apply_flag = $(" [name=\"uc_apply_flag\"]:checked ").val() ;

 if( uc_apply_flag || ( !uc_apply_flag && uc_apply_flag != 0 ) )
            {
                _alert("请选择是否代理功能");
                return;
            }
            $.ajax({
                url: "/userCenter/updSpread.html",
                type: 'POST',
                dataType: 'json',
                data: {
                    uc_apply_flag:uc_apply_flag,
                    s_affect_amount_3dt:amount_3dt,
                    s_affect_amount_ssc:amount_ssc
                },
                success: function(data) {
                    if (data.hasOwnProperty('msg') && data.msg != '' && data.status != 1) {
                        _alert(data.msg);
                    } else {
                        _alert('修改成功!');
                    }
                },
                error:function(data){
                    $.hide_loading();
                }
            });
        });
        //复制按钮
        $("#btn_copy").zclip({
            path:'<?php echo RESOURCE_SITE_URL?>/swf/ZeroClipboard.swf',
            copy:function(){
                return $('[name="spr-site"]').val();
            },afterCopy: function () {
                _alert("复制成功");
            }
        });
    });
Community
  • 1
  • 1
Morgan Ng
  • 813
  • 2
  • 10
  • 22

3 Answers3

0

You could set a default value for radio buttons, so there is no need to check if they are blank or not.

troy
  • 387
  • 1
  • 9
0

Resolved with :

if (!$("input[name='uc_apply_flag']").is(':checked')){
         _alert("请选择是否代理功能");
                return;
}
Morgan Ng
  • 813
  • 2
  • 10
  • 22
-1

Use jQuery.prop to check whether the item has been checked or unchecked instead of $(" [name=\"uc_apply_flag\"]:checked ").val()

Let us know if this worked for you or not.

Samundra
  • 1,869
  • 17
  • 28
  • 1
    I would really appreciate if a reason for negative marking was given. I am still wondering why someone decides to give the negative marking without any reason. – Samundra Aug 27 '16 at 06:56