检查单选按钮值是否为,然后回显一些HTML

时间:2015-06-10 13:36:13

标签: javascript php html magento

我想检查select字段的值是否为某个值,如果是,则需要回显一些文本。

我将此代码用于以下形式的输入框:

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

我现在用这一行回显输入框的整个值:

<div id="reviewwriter">
   <span class="recommendation">
      <?php echo $this->getAnswer() ?>
   </span>
</div>

代码由这个php加载:

public function confRecommendItemsArray()
{
    $resArray = array();
    if (Mage::getStoreConfig('advancedreviews/recommend_options/recommend_field1')) {
        $resArray[] = array(
            'value' => 1,
            'label' => Mage::getStoreConfig('advancedreviews/recommend_options/recommend_field1')
        );
    }
    if (Mage::getStoreConfig('advancedreviews/recommend_options/recommend_field2')) {
        $resArray[] = array(
            'value' => 2,
            'label' => Mage::getStoreConfig('advancedreviews/recommend_options/recommend_field2')
        );
    }

class AW_AdvancedReviews_Block_Recommend_Field extends Mage_Core_Block_Template
{
    public function canShow()
    {
        return (Mage::helper('advancedreviews')->confShowRecommend()
            && count(Mage::helper('advancedreviews')->confRecommendItemsArray()));
    }

    public function getOptions()
    {
        return Mage::helper('advancedreviews')->confRecommendItemsArray();
    }
}

选择字段的值为
1.是
2.否

我想检查值是否为是,如果是,则回显'值为是' 如果值不是echo''。

另见这个JSFiddle:http://jsfiddle.net/wL3xu9d7/1/ 但我不知道为什么它不起作用。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

我希望你想要这个解决方案......

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

</li>

在回答中添加隐藏字段

<div id="reviewwriter">
   <span class="recommendation" id="reviewwriteranswer">
      <?php echo $this->getAnswer() ?>
   </span>

</div>

    <script>
$$(".radio-gender").each(function(el) {
            el.observe("click", function(event) { 
                if(el.checked)
                { 


                sub = $('reviewwriteranswer').value;
              sub ==sub =.trim();
                    if(el.value==sub)
                    {
                        $('reviewwriteranswer').update('value is yes');
                    }else {
                        $('reviewwriteranswer').update('value is No');
                    }
                }
            });
        });

</script>
<scrip>
var allElements = document.body.getElementsByTagName("*");
for(var i = 0; i < allElements.length; i++) {
    var text = allElements[i].innerHTML;
    text=text.trim();

    if (text == 'Yes') {
        allElements[i].innerHTML = "Value is Yes";
    }
    if (text == 'No') {
        allElements[i].innerHTML = "Value is No";
    }
}
</scrip>