Magento:根据所选国家/地区隐藏/显示输入字段

Magento: Hide/Show input field based on Selected Country

本文关键字:地区 隐藏 输入 字段 显示 国家 Magento      更新时间:2023-09-26

我想隐藏/显示|| 根据 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>

国家选择 getCountryHtmlSelect 函数是一个内置的法师函数:

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();
}
<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