用于单选按钮的jQuery无法正常工作

时间:2017-09-26 10:31:41

标签: javascript jquery radio-button smarty

我的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');


        }
    });

1 个答案:

答案 0 :(得分:1)

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

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

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

我已对此声明发表评论:$('#sponsor_user_name').trigger('blur');

这是更新的HTML。我添加了一个新的文本字段&#34; sponsor_user_name&#34;测试结果。

  <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&amp; SPONSOR_NAME

谢谢。