2013-08-22 26 views
-1

這是可能的,在Magento中,創建兩個或多個相關的選擇? 例如國家/地區 我嘗試使用jQuery,但似乎不起作用。 問候相關select在Magento中我該怎麼做

我嘗試用這個代碼:在Mymodule中輸出

應用程序/設計/前端/基/默認/模板/ mymodule中/ mymodule.phtml

<div id="continenti"> 
<?php 
include_once 'option.class.php'; 
$obj = new Option(); 
$obj->ShowContinenti(); 
?> 
</div> 

<div id="nazioni"> 
Seleziona una nazione:<br> 
<select id="sel_nazioni" name="sel_nazioni"><option value="no">Scegli...</option> 
</select> 
</div> 

<div id="result"></div> 

在我的應用程序/設計/前端/基/默認/模板/頁/ HTML/head.phtml

<script type="text/javascript" src="./jquery/jquery-1.3.2.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 

$('#sel_continenti').change(function(){ 

      var cont = $('#sel_continenti').attr('value'); 

      $.post("selection.php", {id_cont:cont}, function(data){ 
      $("#sel_nazioni").empty(); 
      $("div#result").empty(); 
      $("#sel_nazioni").prepend(data); 
      }); 
      }); 

$('#sel_nazioni').change(function(){ 

      var id_naz = $('#sel_nazioni').attr('value'); 

      $.post("result.php", {id:id_naz}, function(data){ 
      $("div#result").empty(); 
      $("div#result").prepend(data); 
      }); 
      }); 

}); 
</script> 

在我的應用程序/代碼/本地/ frontname/mymodule中/ SQL/mysql4安裝-0.1.0

<?php 
$installer = $this; 

$installer->startSetup(); 

$installer->run(" 

    -- DROP TABLE IF EXISTS {$this->getTable('continenti')}; 
    CREATE TABLE {$this->getTable('continenti')} 
      `id` INT(2) UNSIGNED NOT NULL AUTO_INCREMENT, 
      `continente` VARCHAR(40) NOT NULL, 
      PRIMARY KEY (`id`) 
     ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; 

     INSERT INTO `continenti` (`id`, `continente`) VALUES 
     (1, 'Europa'), 
     (2, 'Africa'), 
     (3, 'Sud America'); 

     -- DROP TABLE IF EXISTS {$this->getTable('nazioni')}; 
    CREATE TABLE {$this->getTable('nazioni')} 
      `id` INT(3) UNSIGNED NOT NULL AUTO_INCREMENT, 
      `id_cont` INT(2) UNSIGNED NOT NULL, 
      `nazione` VARCHAR(50) NOT NULL, 
      PRIMARY KEY (`id`) 
     ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ; 

     INSERT INTO {$this->getTable('nazioni')} (`id`, `id_cont`, `nazione`) VALUES 
     (1, 1, 'Spagna'), 
     (2, 1, 'Francia'), 
     (3, 1, 'Italia'), 
     (4, 1, 'Germania'), 
     (5, 1, 'Belgio'), 
     (6, 2, 'Egitto'), 
     (7, 2, 'Marocco'), 
     (8, 2, 'Tunisia'), 
     (9, 2, 'Uganda'), 
     (10, 3, 'Argentina'), 
     (11, 3, 'Cile'), 
     (13, 3, 'Brasile'); 

    ") 

    ; 




    $installer->endSetup(); 

文件result.php

<?php 
include_once 'option.class.php'; 
$obj = new Option(); 
$obj->ShowResult(); 
?> 

文件selection.php

<?php 
include_once 'option.class.php'; 
$obj = new Option(); 
$obj->ShowNazioni(); 
?> 

的第一選擇似乎與區域表的內容,但是,如果我點擊的choise一個區域,第二選擇不會在其內部加載數據。

編輯:解決! 我改變我的head.phtml在

<script type="text/javascript" src="./jquery/jquery-1.3.2.js"></script> 
<script type="text/javascript"> 
jQuery(document).ready(function() { 

jQuery('#sel_continenti').change(function(){ 

      var cont = jQuery('#sel_continenti').attr('value'); 

      jQuery.post("selection.php", {id_cont:cont}, function(data){ 
      jQuery("#sel_nazioni").empty(); 
      jQuery("div#result").empty(); 
      jQuery("#sel_nazioni").prepend(data); 
      }); 
      }); 

jQuery('#sel_nazioni').change(function(){ 

      var id_naz = jQuery('#sel_nazioni').attr('value'); 

      jQuery.post("result.php", {id:id_naz}, function(data){ 
      jQuery("div#result").empty(); 
      jQuery("div#result").prepend(data); 
      }); 
      }); 

}); 
</script> 

我改變$ jQuery的字!

+0

把一些代碼。你已經嘗試了什麼? – Guerra

+0

我已發佈代碼!你可以幫我嗎? – Luigi

回答

0

Magento在結帳地址已經有這種選擇地址phtmls,只是它使用了Prototype。

<div class="field"> 
<label for="billing:region_id" class="required"><em>*</em><?php echo $this->__('State/Province') ?></label> 
<div class="input-box"> 
    <select id="billing:region_id" name="billing[region_id]" title="<?php echo $this->__('State/Province') ?>" class="validate-select" style="display:none;"> 
     <option value=""><?php echo $this->__('Please select region, state or province') ?></option> 
    </select> 
    <script type="text/javascript"> 
    //<![CDATA[ 
     $('billing:region_id').setAttribute('defaultValue', "<?php echo $this->getAddress()->getRegionId() ?>"); 
    //]]> 
    </script> 
    <input type="text" id="billing:region" name="billing[region]" value="<?php echo $this->escapeHtml($this->getAddress()->getRegion()) ?>" title="<?php echo $this->__('State/Province') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('region') ?>" style="display:none;" /> 
</div> 

<div class="field"> 
<label for="billing:country_id" class="required"><em>*</em><?php echo $this->__('Country') ?></label> 
<div class="input-box"> 
    <?php echo $this->getCountryHtmlSelect('billing') ?> 
</div> 

var billingRegionUpdater = new RegionUpdater('billing:country_id', 'billing:region', 'billing:region_id', <?php echo $this->helper('directory')->getRegionJson() ?>, undefined, 'billing:postcode'); 
+0

感謝您的回答! 如果我想在我自己的輸出模塊中選擇其他類型的品牌和版本,我該怎麼做? – Luigi

+0

取決於你知道多少javascript,你可以使用我的例子,在'js/varien/form.js'裏面查看類RegionUpdater,並在你自己的js文件中做一個函數更新,就像它的方法一樣。你可以用你的選擇選項回顯一個json(在一個phtml文件中),並用它的值進行更新,或者做一個ajax調用來檢索這些值,Magento使用回顯的json。 – Emi

+0

謝謝Emi,我發佈了我的代碼,我認爲這可以是有益的! – Luigi