【问题标题】:Default checked button默认选中按钮
【发布时间】:2017-01-13 22:46:17
【问题描述】:

下面的代码是从 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;
})();

【问题讨论】:

  • 在输入中添加checked属性
  • 你不能那样做是什么意思?
  • 自定义字段由 opencart 2 的管理员创建
  • 我认为@KostasKon 真正要问的问题是:如何在 Opencart 2 中配置一组按钮中的默认选中单选按钮。

标签: javascript html radio-button


【解决方案1】:
<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/

【讨论】:

  • 我无法将 id 添加到代码中。是 opencart 和自定义字段。
  • 然后你可以在html代码中添加checked属性
  • 或者你可以使用 getElementsByName("custom_field[1]").checked=true;
  • Opencart 是用 php 编写的。自定义字段是从管理区域而不是代码。
  • 试着告诉我
【解决方案2】:

你可以简单地添加checked属性

&lt;input type="radio" value="2" name="custom_field[1]" checked&gt;

【讨论】:

  • 从 php (opencart) 生成的代码,我使用自定义字段。我无法添加“已检查”
【解决方案3】:

使用 javascript 的解决方案是以下代码:

document.getElementsByName("custom_field[1]")[0].checked=tru‌​e;

谢谢Pandiyan Cool

【讨论】:

  • 你没有转发答案!它将创建重复项。您可以单击原始答案本身旁边的勾号,这对您有帮助
猜你喜欢
  • 2017-10-02
  • 2018-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 2018-04-21
  • 1970-01-01
  • 2013-03-01
相关资源
最近更新 更多