【问题标题】:How do I set the value for a radio button group in Php using DOMDocument如何使用 DOMDocument 在 PHP 中设置单选按钮组的值
【发布时间】:2016-07-12 07:19:20
【问题描述】:

我已经使用 DOMDocument 加载了一些 html,如下所示:

$dom = new DOMDocument();           
$dom->loadHTML($somehtml);
foreach($dom->getElementsByTagName('*') as $element ){
  if($element->getAttribute("type")=='radio') {
    // I need to set the radio group checked value
  }
}

每组有 3 个单选按钮,每组都有相同的名称。如何将特定单选按钮的选中属性设置为 true?我假设我需要以某种方式访问​​集合?

【问题讨论】:

标签: php dom radio-button


【解决方案1】:

这是我的做法。

$radio_10 = $html->createElement('input');
$radio_10->setAttribute('onclick', 'ltgGetFire(false);');
$radio_10_attribute = $html->createAttribute('type');
$radio_10_attribute->value = 'radio';
$radio_10->appendChild($radio_10_attribute);
$radio_10_attribute = $html->createAttribute('name');
$radio_10_attribute->value = 'radius';
$radio_10_attribute = $html->createAttribute('checked');

$radio_10->appendChild($radio_10_attribute);
$radio_10_attribute = $html->createAttribute('id');
$radio_10_attribute->value = 'radius_10';
$radio_10->appendChild($radio_10_attribute);

【讨论】:

    【解决方案2】:

    在这里查看答案... How to select a radio button by default?

    <input type="radio" name="imgsel"  value="" checked="checked" />
    

    【讨论】:

    • 这对于常规的静态 html 来说很好,我需要使用 domDocument 在 Php 中动态设置选中的属性。
    猜你喜欢
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多