gpt4 book ai didi

javascript - 默认选中按钮

转载 作者:行者123 更新时间:2023-11-28 02:50:51 25 4
gpt4 key购买 nike

下面的代码是从 opencart 2 生成的 html

加载页面时没有选择单选按钮。

我如何选择 Value="2"作为默认选中项。 (JavaScript 或 CSS)

<div id="payment-custom-field1" class="form-group custom-field" data-sort="0">
<label class="control-label">Invoice</label>
<div id="input-payment-custom-field1">
<div class="radio">
<label>
<input type="radio" value="2" name="custom_field[1]">
Receipt
</label>
</div>
<div class="radio">
<label>
<input type="radio" value="1" name="custom_field[1]">
Invoice
</label>
</div>
</div>
</div>

用javascript解决

(function() {
document.getElementsByName("custom_field[1]")[0].checked=true;
})();

最佳答案

<input type="radio" id="sample" value="2" name="custom_field[1]">
Receipt
</label>

使用javascript:

document.getElementById("sample").checked = true;

使用 html: 您可以简单地将 checked 属性添加到您的 html 元素

使用元素名称:

(function() {
document.getElementsByName("custom_field[1]")[0].checked=true;
})();

https://jsfiddle.net/pandiyancool/vb73q59w/

关于javascript - 默认选中按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39349641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com