2016-11-21 70 views
0

我有一些頁面的表單,當我提交它時,會將一些數據加載到POST。然後它將用戶鏈接到下一頁。在這個頁面上,我從POST中獲取數據,並且我有兩個dropdownlists,第二個依賴於第一個。從POST數據第一個GET值:需要從鏈接列表中獲得一些變量的值

echo '<script type="text/javascript"> 
jQuery("#markid").val("'.$GLOBALS["i"].'"); </script>'; 

其中$ GLOBALS [ 「我」] =從DB,已經由之前的頁面保存在數據從帖子的ID。 但它不依賴於它的第二個下拉列表工作:

echo '<script type="text/javascript"> 
    jQuery("#comm").val("'.$GLOBALS["i1"].'"); </script>'; 

我認爲它可以從代碼的一部分,從而實現根據第二個下拉列表中:

<script> 
jQuery(function(){ 
var id = jQuery(".mark").val(); 
jQuery.ajax({ 
    type:"POST", 
    url: "wp-content/com.php", 
    data: {id_mark: id}, 
    success: function(data){ 
jQuery(".comm").html(data); 
} 
}); 
     jQuery(".mark").change(function(){ 
var id = jQuery(".mark").val(); 
if(id==0){ 
} 
jQuery.ajax({ 
    type:"POST", 
    url: "wp-content/com.php", 
    data: {id_mark: id}, 
    success: function(data){ 
      jQuery(".comm").html(data); 
} 
}); 
}); 

其中「標記」 - 第一個下拉列表,「通信」 - 第二個。 這是我的問題的第一部分。 第二:我在頁面上有一些值取決於第二個下拉列表的值。我試圖:

jQuery(".comm").change(function(){ 
var id = jQuery(".comm").val(); 
if(id==0){ 
} 
jQuery.ajax({ 
    type:"POST", 
    url: "wp-content/pricecar.php", 
    data: {id_mark: id}, 
    success: function(data){ 
      jQuery(".price9").html(data); 
var price1 = jQuery(".price1").val(); 
var price2 = jQuery(".price2").val(); 
var price3 = jQuery(".price3").val(); 
var price4 = jQuery(".price4").val(); 
var price5 = jQuery(".price5").val(); 
var price6 = jQuery(".price6").val(); 
var price7 = jQuery(".price7").val(); 
var price8 = jQuery(".price8").val(); 
var price9 = jQuery(".price9").val(); 
jQuery.ajax({ 
      type:"POST", 
      url: "../wp-content/price.php", 
      data: {price1: price1,price2: price2,price3: price3,price4: price4,price5: price5,price6: price6,price7: price7,price8: price8, price9: data}, 
      success: function(data){ 
       jQuery(".summPrice").html(data); 
      } 
}); 
          } 
      }); 

但它只有一次,我不知道爲什麼。 我會很高興爲任何優惠。

+0

你能發佈php提供的相關html嗎?你如何填寫下拉菜單?直接從PHP或與另一個Ajax調用? – Daniele

+0

''和第二個你在代碼中看到我在下面看到的,看看:'jQuery(」。mark「)。change(function(){' –

回答

0

我沒有呈現的HTML和Ajax響應的全面瞭解,但我會給一個嘗試:

刪除此行

echo '<script type="text/javascript"> 
jQuery("#markid").val("'.$GLOBALS["i"].'"); 
</script>'; 

echo '<script type="text/javascript"> 
    jQuery("#comm").val("'.$GLOBALS["i1"].'"); </script>'; 

,做這樣的事情哪裏您打印HTML

... 
<select id="markid" data-val="<?php echo isset($GLOBALS["i"]) ? $GLOBALS["i"] : -1;?>"></select> 
<select id="comm" data-val="<?php echo isset($GLOBALS["i1"]) ? $GLOBALS["i1"] : -1;?>"></select> 

而且在你的JavaScript有類似

<script> 
(function ($) { 
    //when everything is ready 
    $(fillUpCommOptions); 
    $(watchSelectsChanges); 

    function fillUpCommOptions() { 
     var id = $('#markid').data('val') ? $('#markid').data('val') : $('#markid').val(); 
     $('#markid').removeAttr('data-val');//remove data-val, at next change event we want the select new val 
     $.ajax({ 
      type: "POST", 
      url: "wp-content/com.php", 
      data: {id_mark: id}, 
      success: function (data) { 
       //assuming data is something like 
       // '<option value="niceValue">nice value</option> 
       $("#comm").html(data); 
       if ($("#comm").data('val')) { 
        //apply values from post also for the second dropdown 
        // assuming that data contains and option with value == $("#comm").data('val') 
        $("#comm").val($("#comm").data('val')); 
        $('#comm').removeAttr('data-val'); 

        $("#comm").change()//trigger change after setting the value 
       } 

      } 
     }); 
    } 

    function watchSelectsChanges() { 
     $('#markid') 
      .off('change')//just in case, could not be needed 
      .on('change', fillUpCommOptions); 

     $('#comm') 
      .off('change')//just in case, could not be needed 
      .on('change', handleDependentValues); 
    } 

    function handleDependentValues() { 
     var id = $("#comm").val(); 
     if (id) { 
      $.ajax({ 
       type: "POST", 
       url: "wp-content/pricecar.php", 
       data: {id_mark: id}, 
       success: function (data) { 
        jQuery(".price9").html(data); 
        var price1 = jQuery(".price1").val(); 
        var price2 = jQuery(".price2").val(); 
        var price3 = jQuery(".price3").val(); 
        var price4 = jQuery(".price4").val(); 
        var price5 = jQuery(".price5").val(); 
        var price6 = jQuery(".price6").val(); 
        var price7 = jQuery(".price7").val(); 
        var price8 = jQuery(".price8").val(); 
        var price9 = jQuery(".price9").val(); 
        jQuery.ajax({ 
         type: "POST", 
         url: "../wp-content/price.php", 
         data: { 
          price1: price1, 
          price2: price2, 
          price3: price3, 
          price4: price4, 
          price5: price5, 
          price6: price6, 
          price7: price7, 
          price8: price8, 
          price9: data 
         }, 
         success: function (data) { 
          jQuery(".summPrice").html(data); 
         } 
        }); 
       } 
      }) 
     } 
    } 

})(jQuery);