2016-03-15 58 views
0

所以我想修改opencart 2.x的默認註冊帳戶頁面。我使用jquery replaceWith將城市input更改爲select,因此訪問者可以從列表中選擇城市。在訪客選擇城市後,應在區域select上添加選項列表,以便訪客可以選擇地區。但這不起作用。這些mya代碼:jquery select onchange - opencart 2.x註冊帳戶頁面

<script type="text/javascript"><!-- 
$('select[name=\'country_id\']').on('change', function() { 
     var selected_country = this.value; 
     $.ajax({ 
      url: 'index.php?route=account/account/country&country_id=' + this.value, 
      dataType: 'json', 
      beforeSend: function() { 
       $('select[name=\'country_id\']').after(' <i class="fa fa-circle-o-notch fa-spin"></i>'); 
      }, 
      complete: function() { 
       $('.fa-spin').remove(); 
      }, 
      success: function (json) { 
       if (selected_country == 100) { 
        //if country indonesia is selected, we use dropdown to fetch city list (ajax) 
        select_html = '<select name="city_id" id="input-city" class="form-control"><option value=""><?php echo $text_select;?></option></select>'; 
        $('input[name=\'city\']').replaceWith(select_html); 
        $('#kecamatan').show(); 
        $('#desa').show(); 
       } else { 
        //we don't know all of cities in the world :/ 
        input_html = '<input type="text" name="city" value="<?php echo $city; ?>" placeholder="<?php echo $entry_city; ?>" id="input-city" class="form-control" />'; 
        $('select[name=\'city_id\']').replaceWith(input_html); 
        $('#kecamatan').hide(); 
        $('#desa').hide(); 
       } 

       if (json['postcode_required'] == '1') { 
        $('input[name=\'postcode\']').parent().parent().addClass('required'); 
       } else { 
        $('input[name=\'postcode\']').parent().parent().removeClass('required'); 
       } 

       html = '<option value=""><?php echo $text_select; ?></option>'; 

       if (json['zone']) { 
        for (i = 0; i < json['zone'].length; i++) { 
         html += '<option value="' + json['zone'][i]['zone_id'] + '"'; 

         if (json['zone'][i]['zone_id'] == '<?php echo $zone_id; ?>') { 
          html += ' selected="selected"'; 
         } 

         html += '>' + json['zone'][i]['name'] + '</option>'; 
        } 
       } else { 
        html += '<option value="0" selected="selected"><?php echo $text_none; ?></option>'; 
       } 

       $('select[name=\'zone_id\']').html(html); 
      }, 
      error: function (xhr, ajaxOptions, thrownError) { 
       alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); 
      } 
     }); 
    }); 

    $('select[name=\'country_id\']').trigger('change'); 
//--></script> 
<script type="text/javascript"> 
    $('select[name=\'zone_id\']').on('change', function() { 
     $.ajax({ 
      url: 'index.php?route=localisation/indonesia/kabupaten&oc_zone_id=' + this.value, 
      dataType: 'json', 
      beforeSend: function() { 
       $('select[name=\'zone_id\']').after(' <i class="fa fa-circle-o-notch fa-spin"></i>'); 
      }, 
      complete: function() { 
       $('.fa-spin').remove(); 
      }, 
      success: function (json) { 
       if (json['postcode_required'] == '1') { 
        $('input[name=\'postcode\']').parent().parent().addClass('required'); 
       } else { 
        $('input[name=\'postcode\']').parent().parent().removeClass('required'); 
       } 

       html = '<option value=""><?php echo $text_select; ?></option>'; 

       if (json['kabupaten']) { 
        for (i = 0; i < json['kabupaten'].length; i++) { 
         html += '<option value="' + json['kabupaten'][i]['id'] + '"'; 

         if (json['kabupaten'][i]['id'] == '<?php echo $city; ?>') { 
          html += ' selected="selected"'; 
         } 

         html += '>' + json['kabupaten'][i]['nama'] + '</option>'; 
        } 
       } 
       $('select[name=\'city_id\']').html(html); 
      }, 
      error: function (xhr, ajaxOptions, thrownError) { 
       alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); 
      } 
     }); 
    }); 
    $('select[name=\'zone_id\']').trigger('change'); 
</script> 
<script type="text/javascript"> 
    $('select[name=\'city_id\']').on('change', function() { 
     $.ajax({ 
      url: 'index.php?route=localisation/indonesia/kecamatan&id_kabupaten=' + this.value, 
      dataType: 'json', 
      beforeSend: function() { 
       $('select[name=\'city_id\']').after(' <i class="fa fa-circle-o-notch fa-spin"></i>'); 
      }, 
      complete: function() { 
       $('.fa-spin').remove(); 
      }, 
      success: function (json) { 
       if (json['postcode_required'] == '1') { 
        $('input[name=\'postcode\']').parent().parent().addClass('required'); 
       } else { 
        $('input[name=\'postcode\']').parent().parent().removeClass('required'); 
       } 

       html = '<option value=""><?php echo $text_select; ?></option>'; 

       if (json['kecamatan']) { 
        for (i = 0; i < json['kecamatan'].length; i++) { 
         html += '<option value="' + json['kecamatan'][i]['id'] + '"'; 

         if (json['kecamatan'][i]['id'] == '<?php echo $city; ?>') { 
          html += ' selected="selected"'; 
         } 

         html += '>' + json['kecamatan'][i]['nama'] + '</option>'; 
        } 
       } 
       $('select[name=\'kecamatan_id\']').html(html); 
      }, 
      error: function (xhr, ajaxOptions, thrownError) { 
       alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); 
      } 
     }); 
    }); 
    $('select[name=\'city_id\']').trigger('change'); 
</script> 

我試着在這裏創建簡單的腳本https://jsfiddle.net/oo42kq9z/並且工作。我認爲它是相似的,但爲什麼在登錄頁面$('select[name=\'city_id\']').trigger('change')無法觸發?

回答

0

假設你select city_id has the id of 'city_id'你將不得不使用$(document).on("change")

$(document).on("change", "#city_id", function(){ 
/* Code Goes Here */ 
} 
+0

那的工作,謝謝。 '$('select [name ='country_id']')'和$('select [name ='zone_id']')'工作,爲什麼'$('select [name = \'city_id \ ']')'不起作用?任何解釋? –

+0

很可能是因爲'id ='city_id''沒有設置'city_id'。你能證實嗎? –

+0

'//如果國家被選中,用下拉式替換輸入以獲取城市列表(ajax) select_html =''; ('input'[name ='city''')。replaceWith(select_html);'所以我使用'$(document).on('change','#input-city',function(){} );' –