【问题标题】:Magento: Hide/Show input field based on Selected CountryMagento:根据所选国家/地区隐藏/显示输入字段
【发布时间】:2016-04-25 10:34:30
【问题描述】:

我想隐藏/显示 ||根据 magento 结帐页面中所选国家/地区禁用/启用输入字段。我尝试将此代码与 magento 分开,它运行良好,但是当我将它应用到我的 magento 时,它无法正常工作。我刚刚从 IWD 结帐扩展程序中编辑了 billing.phtml,而没有从 magento 核心中编辑。

这里是javascript:

$(function() {
var $cid = $(document.getElementById("billing:country_id")), // Country ID in magento
$custom = $(document.getElementById("billing:custom")); //my custom input field

$cid.change(function() {
    if ($cid.val() == 'US') {
        $custom.removeAttr('disabled');
        alert('working1');
    } else {
        $custom.attr('disabled', 'disabled').val('');
        alert('working2');
    }
}).trigger('change');
});

这是来自 IWD / Magento 的示例计费表单模板:

<li class="fields">
     <label for="billing:country_id" class="required"><em>*</em><?php echo $this->__('Country') ?></label>
     <div class="input-box">
         <?php echo $this->getCountryHtmlSelect('billing') ?>
     </div>
</li>
<li class="fields">
      <label for="billing:custom" class="required"><em>*</em><?php echo $this->__('Custom Field') ?></label>
      <div class="input-box">
            <input type="text" name="custom[custom]" value="<?php echo $this->htmlEscape($this->getQuote()->getCustom()) ?>" title="<?php echo $this->__('Custom Field') ?>" class="input-text custom_id" id="billing:custom" />
       </div>
 </li>

Country Select getCountryHtmlSelect 函数是一个内置的 mage 函数:

public function getCountryHtmlSelect($type)
{
    $countryId = $this->getAddress()->getCountryId();
    if (is_null($countryId)) {
        $countryId = Mage::helper('core')->getDefaultCountry();
    }
    $select = $this->getLayout()->createBlock('core/html_select')
        ->setName($type.'[country_id]')
        ->setId($type.':country_id')
        ->setTitle(Mage::helper('checkout')->__('Country'))
        ->setClass('validate-select')
        ->setValue($countryId)
        ->setOptions($this->getCountryOptions());
    if ($type === 'shipping') {
        $select->setExtraParams('onchange="if(window.shipping)shipping.setSameAsBilling(false);"');
    }

    return $select->getHtml();
}

【问题讨论】:

    标签: javascript html magento


    【解决方案1】:
    <input id="billing:country_id"></input> 
    <input id="billing:custom" disabled="disabled;"></input>
    

    代码

    $(function() {
    var $cid = $(document.getElementById("billing:country_id"));
    $custom = $(document.getElementById("billing:custom")); 
    
    $cid.change(function() {
        if ($cid.val() == 'US') {
            $custom.prop('disabled', false);
            console.log('working1');
        } else {
            $custom.prop('disabled', true).val("");
            console.log('working2');
        }
    }).trigger('change');
    });
    

    http://codepen.io/anon/pen/dMqYgK?editors=1011

    【讨论】:

    • 我为此创建了一个新的 js 文件并添加到我的 magento page.xml 中(用于快速测试)。不幸的是,没有运气。我编辑了我的问题以显示来自 magento 的示例表单模板。而且,这个脚本在 magento 中工作: jQuery('#billing\\:country_id').val('US'); jQuery('#billing\\:country_id').prop('disabled','disabled'); 我只是想知道这里如何创建条件。
    猜你喜欢
    • 2021-10-27
    • 2012-06-15
    • 1970-01-01
    • 2021-10-01
    • 2022-01-18
    • 2020-04-12
    • 2021-11-22
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多