【问题标题】:jquery: setting radio inout as checked [duplicate]jquery:将无线电输入设置为选中[重复]
【发布时间】:2018-07-08 11:30:14
【问题描述】:

这是很基本的,但我无法理解。

在捐赠表格上,人们可以选择预设的金额选项或“其他”。如果他们选择“其他”,即使选择了预设,也必须激活“其他”单选按钮。

HTML:

<label>
<input type="radio" name="x_ccPayment" class="required" title="*" 
    value="120" data-payment="once" />
$120</label>


<label>
<input type="radio" name="x_ccPayment" class="required" title="*"
    value="50" data-payment="once" />
$50</label>

<label>
<input type="radio" name="x_ccPayment" class="required" title="*"
    value="-99" data-payment="once" />
Other:</label>
<input type="text" name="x_ccPayment_other" id="other_once" style="width: 100px;" value="" onKeyUp="checkNumericInput(this)"  data-payment="once" class="form-control" />

查询:

 $("#other_once").keyup(function(){
    var other_once =  $("#other_once").val();
    if (other_once !=='') {
        $("input[name=x_ccPayment]").removeAttr('checked'); // deselect other options
        $("input[name=x_ccPayment][data-payment=once][value=-99]").attr('checked','checked');  /auto-select the 'other' radio
    }

 });

当我查看开发工具时,我看到收音机是 checked 正确,但屏幕上没有显示。

【问题讨论】:

标签: jquery


【解决方案1】:

不要将.removeAttr().attr() 用于选中的属性。使用.prop():

$("input[name=x_ccPayment]").prop('checked',false); // deselect other options
$("input[name=x_ccPayment][data-payment=once][value=-99]").prop('checked',true);  

【讨论】:

    猜你喜欢
    • 2015-10-26
    • 1970-01-01
    • 2014-05-11
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2014-09-01
    • 2016-11-19
    相关资源
    最近更新 更多