jquery radio改变了data属性

时间:2015-07-17 17:17:54

标签: jquery

我的代码中有这样的广播组:

<div id="question_1">
    <label for="q1_1a"><input name="q[1][1]" id="q1_1a" type="radio">A</label>
    <label for="q1_1b"><input name="q[1][1]" id="q1_1b" type="radio">B</label>
    <label for="q1_1c"><input name="q[1][1]" id="q1_1c" type="radio" data-target="#q1_2">Other</label>
        <input id="q1_2" name="q[1][2]" type="text" placeholder="specify">
</div>
<div id="question_2">
    <label for="q2_1a"><input name="q[2][1]" id="q2_1a" type="radio">A</label>
    <label for="q2_1b"><input name="q[2][1]" id="q2_1b" type="radio">B</label>
    <label for="q2_1c"><input name="q[2][1]" id="q2_1c" type="radio" data-target="#q2_2hide">Other</label>
    <div id="q2_2hide">
        Specify:
        <input name="q[2][2]" type="text">
    </div>
</div>

依旧......

当我检查包含data-target属性的单选按钮时,我试图编写将显示数据目标字段的jquery代码。到目前为止,我有这样的事情:

$('input[type="radio"][data-target]').change(function () { 
    $($(this).data('target')).toggle($(this).is(':checked'));
});

它会显示字段,但是一旦您选择了其他选项,它就不会隐藏它。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

那是因为onchange事件仅在检查时针对单选按钮触发,而不是在未选中时触发。 以下是处理预期行为的更通用方法:

In [6]:  check_cuda()
------------------------------libraries detection-------------------------------
Finding cublas
    located at ~/.anaconda3/lib/libcublas.so.7.0.28
    trying to open library...   ok
Finding cusparse
    located at ~/.anaconda3/lib/libcusparse.so.7.0.28
    trying to open library...   ok
Finding cufft
    located at ~/.anaconda3/lib/libcufft.so.7.0.35
    trying to open library...   ok
Finding curand
    located at ~/.anaconda3/lib/libcurand.so.7.0.28
    trying to open library...   ok
Finding nvvm
    located at ~/.anaconda3/lib/libnvvm.so.3.0.0
    trying to open library...   ok
    finding libdevice for compute_20... ok
    finding libdevice for compute_30... ok
    finding libdevice for compute_35... ok
-------------------------------hardware detection-------------------------------
Found 1 CUDA devices
id 0      b'GeForce GTX 970'                              [SUPPORTED]
                      compute capability: 5.2
                           pci device id: 0
                              pci bus id: 7
Summary:
    1/1 devices are supported
PASSED
Out[6]:  True

-DEMO-