简体   繁体   中英

Change input select to radio

I want to change my select code to radio buttons:

My current code is:

<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>

I tried to change it into this:

<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>

What am I doing wrong?

Currently I can select both values and it should only be possible to select 1 radio button.

Just give same name to all your radio buttons

Your code would look like

<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>

您需要在单选按钮的name属性中添加单选组的name

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

you forgot to add name attribute

<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>

I have added name attribute for radio button, you are not added.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM