2016-04-25 14 views
0

私は非表示にしたいマジェンタチェックアウトページで選択した国に基づいて入力フィールドを無効/有効にします。私はこのコードを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(); 
} 

答えて

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

+0

私はこのための新しいJSファイルを作成し、(高速テスト用)私のMagentoのpage.xmlに追加しました。残念ながら、運はありません。 Magentoからサンプルフォームテンプレートを表示するために質問を編集しました。また、このスクリプトはマゼンタの中で動作します: _ ** jQuery( '#billing \\:country_id')val( 'US');jQuery( '#billing \\:country_id')。prop( 'disabled'、 'disabled'); ** _ここで条件を作成する方法は不思議です。 – Romanov

関連する問題