2016-11-21 46 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);