Javascript jQuery autoselect radiobox不工作

Javascript jQuery autoselect radiobox不工作,javascript,jquery,Javascript,Jquery,我无法使用jquery代码自动选择radiobox。 这是我的html: <div class="formField rsform-block rsform-block-existingcustomer" style="margin-bottom: -22px;"> <!--<input name="form[existingCustomer]" type="radio" value="Yes" id="existingCustomer0" /><

我无法使用jquery代码自动选择radiobox。 这是我的html:

<div class="formField rsform-block rsform-block-existingcustomer" style="margin-bottom: -22px;">

    <!--<input  name="form[existingCustomer]" type="radio" value="Yes" id="existingCustomer0"  /><label for="existingCustomer0">Yes</label><input checked="checked" name="form[existingCustomer]" type="radio" value="No" id="existingCustomer1"  /><label for="existingCustomer1">No</label><br/>
    <span id="component100" class="formNoError">Please tell us if you're an existing customer.</span>-->

    Are you an existing client?<br>
    <label for="existingCustomer0" class="radio"><span class="icon"></span><span class="icon-to-fade"></span>Yes
    <input name="form[existingCustomer]" type="radio" value="Yes" id="existingCustomer0" class="addRadio">
    </label>
    <label for="existingCustomer1" class="radio checked"><span class="icon"></span><span class="icon-to-fade"></span>No
    <input checked="checked" name="form[existingCustomer]" type="radio" value="No" id="existingCustomer1" class="addRadio" style="display:none;">
    </label>
</div>
有人看到问题了吗?我正在尝试自动选择“是”复选框,以便它将激活下一部分,即创建下拉菜单

提前感谢!:)

试试这个:

   $(document).ready(function () {                  
     $('input:radio[name="form[existingCustomer]"][value="Yes"]').attr('checked',true);
     //OR
     $('input:radio[name="form[existingCustomer]"][value="Yes"]').prop('checked',true);
    });

我在这里看到您的代码存在一些问题。 1.输入hmtl没有正确的结束/结束标记 2.不知道你为什么要把它包在标签上 3.确保将jquery代码放在DocumentReady中,以便在加载页面时它检查radiobox。 4.在html代码中,您预先设置了要检查的No radio。那是故意的吗?看起来您将其设置为no,然后使用jquery将其设置回yes

无论如何,尝试attr而不是prop。像这样的

$('input:radio[value="Yes"]').attr('checked', true);

你说的自动选择是什么意思?i、 e在页面加载时,检查此收音机。或者您只想在单击时选择它。在页面加载时,它将被选中。不幸的是,这不起作用。不过,我确实将您的“$”更改为jQuery,但这并不重要。它在控制台日志中也没有显示任何错误…奇怪。是的,您的示例似乎有效,…唯一的区别是您重新构造了html。我有自定义按钮类可供选择。我试着到处玩。是的,它在那里工作,很奇怪它在真实环境中怎么不工作……我试着解决它。我放置了控制台日志,它们都显示出来了…所以它运行代码,没有发现错误…只是没有这样做。尝试使用最新版本的jQuerylet us
$('input:radio[value="Yes"]').attr('checked', true);