2013-07-14 188 views
1

让我们说,我有这样一个div:取消选择单选按钮

<div> 
    <div> 
     <input type="radio" id="cheque" name="type" value="" /> 
     <label for="cheque">Cheque</label> 
    </div><br /> 
    <div> 
     <input type="radio" id="credit_card" name="type" value="" />  
     <label for="credit_card">Credit card</label> 
     <div style="margin-left:45px;"> 
      <label for="visa">Visa</label> 
      <input type="radio" id="visa" name="card_type" value="" /><br /> 

      <label for="mastercard">Mastercard</label> 
      <input type="radio" id="mastercard" name="card_type" value="" /> 
     </div> 
    </div> 
</div> 

正如你可以看到here,用户可以选择支票或信用卡,但假设用户选择签证然后再次选择“再次检查”,Visa单选按钮仍处于选中状态。我不希望这种情况发生。如果用户选择维萨或万事达卡,并且如果用户返回选择支票(选择Visa或万事达卡时),我想要自动选择信用卡,我希望单选按钮信用卡,维萨和万事达卡不被选中。这可以用只有HTML和CSS来完成,否则我必须使用JavaScript来做到这一点?

谢谢

回答

1

不幸的是,HTML中没有“子分组”。

检查定义the <input> element

属性

    type

        radio:单选按钮。您必须使用属性来定义提交本项目的 值。使用检查属性表明 此项目是否被默认选择。单选按钮具有用于name属性 相同的值是相同的“单选按钮 基”中;一次只能选择一个组中的一个单选按钮。

这里没有更多的提组。另外,如果搜索该页面,则唯一的其他分组相关位是<optgroup>,但它仅适用于<option>标记。

对于这一点,你唯一的选择是使用JavaScript为:

document.getElementById('mastercard').onclick = function() { 
    document.getElementById('credit_card').checked = true; 
}; 
document.getElementById('visa').onclick = function() { 
    document.getElementById('credit_card').checked = true; 
}; 
document.getElementById('cheque').onclick = function() { 
    document.getElementById('mastercard').checked = false; 
    document.getElementById('visa').checked = false; 
}; 

Your fiddle, with this code.

1

添加这Java脚本

function cheque_onclick() { 

visa.checked = false; 
mastercard.checked = false; 

} 

function visa_onclick() { 
credit_card.checked = true; 

} 

function mastercard_onclick() { 
credit_card.checked = true; 

}