2017-07-07 34 views
-1

我的问题是,我无法编辑表单代码输出,因此如果要选择不同的单选按钮,可以选择或取消选中无线标识。这是可能的吗? - 我可以在标签上添加一个函数调用或使用jquery - 但我不能像添加onclick事件一样编辑表单代码本身。选中或取消选中所有无线电广播输入ID是否选择

伪代码将是:

If #unsubYes is checked Then 
uncheck #option1Yes + #option2Yes AND 
check #option1No + #option2No 

If #unsubNo is checked Then 
uncheck #option1No + #option2No AND 
check #option1Yes + #option2Yes 

Link to JS fiddle html

任何帮助表示赞赏。

干杯, 克里斯

+0

很可能是这个缘故。你试过什么了? – NewToJS

+0

我可以通过onclick事件来做到这一点,但不知道如何使其发生火灾 – chris

+0

你看过其他事件监听器并试过吗?如果是,那么哪些?你可以包括你的尝试相关的源代码。这会让其他人看到,处理和调试。也许可以解释为什么你的尝试没有按预期运作,然后也提供了一个解决方案来满足你的需求。没有看到你有什么或者你的尝试,就不可能解释它为什么不起作用,或者给你一个可靠的答案/解决方案来学习。 – NewToJS

回答

1

$("[name='unsub']").on('change', function(){ 
 
    var val = $(this).val(); 
 
    var reverseVal = (val == 'Yes' ? 'No' : 'Yes') 
 
    $("input[value='"+reverseVal+"']:not([name='unsub'])").prop('checked', true); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
<span>Option 1</span><br /> 
 
<label> 
 
    <input type="radio" name="option1" id="option1Yes" value="Yes"> 
 
    Yes 
 
</label> 
 
<label> 
 
    <input type="radio" name="option1" id="option1No" value="No"> 
 
    No 
 
</label> 
 
<br /><br /> 
 
<span>Option 2</span><br /> 
 
<label> 
 
    <input type="radio" name="option2" id="option2Yes" value="Yes"> 
 
    Yes 
 
</label> 
 
<label> 
 
    <input type="radio" name="option2" id="option2No" value="No"> 
 
    No 
 
</label> 
 
<hr /> 
 
<span>Unsubscribe from all</span><br /> 
 
<label> 
 
    <input type="radio" name="unsub" id="unsubYes" value="Yes"> 
 
    Yes 
 
</label> 
 
<label> 
 
    <input type="radio" name="unsub" id="unsubNo" value="No"> 
 
    No 
 
</label> 
 
<br /><br /> 
 
<input type="submit" value="submit"> 
 
</form>

+0

这太棒了@EmīlsGulbis - 但是有没有办法改变正在检查的无线电?目前点击取消勾选是勾选是无线电时,它应该勾选没有收音机(反之亦然) – chris

+0

当然,编辑的代码! –

+0

非常感谢!传说 – chris

0

当然有可能。 你可以做类似这样的事情

document.querySelector('#unsubYes').addEventListener('change', function() { 
    if (this.checked) { 
     // do something if is checked 
    } else { 
     // do other thins 
    } 
}); 
相关问题