jQuery 单选按钮不起作用

jQuery for radio button is not working

我的 TPL 文件和单选按钮无法正常工作,我是 jquery 的新手,所以请帮助

 {if ($log_in=='yes') && ($url == NULL) }
        <div class="form-group row">
         <div class="col-sm-4 control-label">
            <input  type="radio" name="account_types" value="downline" checked="true">&nbsp;Downline Member</div>
         <div class="col-sm-4 control-label">
            <input  type="radio" name="account_types" value="MGM">&nbsp;MGM Member</div>            
          <input type="hidden" value="{$sponsor_user_name}" name="sponsor_name" id="sponsor_name">
          <input type="hidden" value="{$sponsors_sponsor_name}" id="sponsors_sponsor_name" name="sponsors_sponsor_name">
           </div><br>
        {/if} {else}<input  type="hidden" name="account_type" value="downline">{/if}

我的 jquery 是

 $('input[type=radio][name=account_types]').on('change', function() {
         alert();
        if (this.value == 'downline') {
            $('#sponsor_user_name').val($('#sponsor_name').val());

            $('#sponsor_user_name').trigger('blur');

        }
        else if (this.value == 'MGM') {
            $('#sponsor_user_name').val($('#sponsors_sponsor_name').val());

            $('#sponsor_user_name').trigger('blur');


        }
    });

我使用您的 HTML 和 JavaScript 创建了一个示例 Fiddle。

你能看看吗?因为,它在那里工作得很好。

https://jsfiddle.net/cs3juct7/1/

这条语句我已经注释掉了:$('#sponsor_user_name').trigger('blur');

此处更新HTML。我添加了一个新的文本字段 "sponsor_user_name" 来测试结果。

  <div class="form-group row">
     <div class="col-sm-4 control-label">
        <input  type="radio" name="account_types" value="downline" checked="true">&nbsp;Downline Member
     </div>
     <div class="col-sm-4 control-label">
        <input  type="radio" name="account_types" value="MGM">&nbsp;MGM Member
     </div>            
     <input type="hidden" value="sponsor_name_value" name="sponsor_name" id="sponsor_name">
     <input type="hidden" value="sponsors_sponsor_name_value" id="sponsors_sponsor_name" name="sponsors_sponsor_name">
  </div><br>
  <input type="text" id="sponsor_user_name" name="sponsor_user_name" />

此处更新JavaScript。

$('input[type=radio][name=account_types]').on('change', function() {
     //alert(this.value);
    if (this.value == 'downline') {
        $('#sponsor_user_name').val($('#sponsor_name').val());

        //$('#sponsor_user_name').trigger('blur');

    }
    else if (this.value == 'MGM') {
        $('#sponsor_user_name').val($('#sponsors_sponsor_name').val());

        //$('#sponsor_user_name').trigger('blur');


    }
});

另请确保您在两个隐藏文本字段中都有一些值。即 sponsors_sponsor_name & sponsor_name

谢谢。