将输入选择更改为收音机

时间:2015-05-27 10:39:42

标签: javascript php jquery radio-button

我想将我的选择代码更改为单选按钮:

我目前的代码是:

<li>
    <label for="recommend_field"><?php echo $this->__('Would you recommend this product to a friend?') ?></label>
    <div class="input-box">
        <select name="recommend"  id="recommend_field" class="input-text" style="width: 360px;">
            <option></option>
            <?php foreach ( $this->getOptions() as $option ): ?>
            <option value="<?php echo $option['value'] ?>"><?php echo $this->__($option['label']) ?></option>
            <?php endforeach ?>
        </select>
    </div>
</li>

我试着把它改成这个:

<li>
    <label for="recommend_field"><?php echo $this->__('Would you recommend this product to a friend?') ?></label>
    <div class="input-box">
            <?php foreach ( $this->getOptions() as $option ): ?>
            <label>
            <input type="radio" class="radio" value="<?php echo $option['value'] ?>"<?php if ($option['value'] == $value) echo ' checked="checked"' ?>><?php echo __($option['label']) ?></input>
            </label>
            <?php endforeach ?>
    </div>
</li>

我做错了什么?

目前我可以选择这两个值,只能选择1个单选按钮。

4 个答案:

答案 0 :(得分:3)

只需为所有单选按钮提供相同的name

您的代码看起来像

<li>
    <label for="recommend_field">
        <?php echo $this->__('Would you recommend this product to a friend?') ?></label>
    <div class="input-box">
        <?php foreach ( $this->getOptions() as $option ): ?>
        <label>
            <input type="radio" class="radio" name="rdoSelect" value="<?php echo $option['value'] ?>" <?php if ($option[ 'value']==$ value) echo ' checked="checked"' ?>>
            <?php echo __($option[ 'label']) ?>
            </input>
        </label>
        <?php endforeach ?>
    </div>
</li>

答案 1 :(得分:0)

您需要在单选按钮的name属性中添加广播组的名称。

<input name="groupname" type="radio" class="radio" value="<?php echo $option['value'] ?>" <?php if ($option[ 'value'] == $ value) echo ' checked="checked"' ?>>

答案 2 :(得分:0)

您忘记添加name属性

<li>
    <label for="recommend_field"><?php echo $this->__('Would you recommend this product to a friend?') ?></label>
    <div class="input-box">
            <?php foreach ( $this->getOptions() as $option ): ?>
            <label>
            <input name="recommend" type="radio" class="radio" value="<?php echo $option['value'] ?>"<?php if ($option['value'] == $value) echo ' checked="checked"' ?>><?php echo __($option['label']) ?></input>
            </label>
            <?php endforeach ?>
    </div>
</li>

答案 3 :(得分:0)

我为单选按钮添加了名称属性,但未添加。

<li>
    <label for="recommend_field">
        <?php echo $this->__('Would you recommend this product to a friend?') ?>
    </label>
    <div class="input-box">
        <?php foreach ( $this->getOptions() as $option ): ?>
        <label>
            <input type="radio" class="radio" name="radio-group" value="<?php echo $option['value'] ?>" <?php if ($option[ 'value']==$ value) echo ' checked="checked"' ?>>
            <?php echo __($option[ 'label']) ?>
            </input>
        </label>
        <?php endforeach ?>
    </div>
</li>