2017-04-02 111 views
1

我有两个通过Ajax传递我的日历值。问题是,日期值只有在第二次尝试选择日期后才会传递,并且它正在传递第一个尝试值!通过Ajax传递日期值

当前结果作为例子:

在第一次尝试,我将选择:2017年3月1日

selectvalueds = ''

在第二次尝试,我将选择: 2015-05-30

Selectvalueds ='2017-03-01'

重要提示:! 由于一些计算,我必须先将日历的值复制到#start,然后将#start通过Ajax传递给我的php代码。 的Javascript:

$('#calendar').change(function() { 
    $('#start').val($(this).val()); 
}); 
    $('#calendar’).change(function(){ 
      var selectvalueds = $('#start').val(); 
      Unit_id.html('<option value="">Loading...</option>'); 
      if (selectvalueds == '') 
      {Unit_id.html(initial_Unit_html);} 
      else 
      { 
       $.ajax({ 
        url: 'index.php', 
        data:'option=com_myapp&task=getUnitsHTML&dsvalue='+selectvalueds, 
        success: function(output) { 
         Unit_id.html(output); 
        }, 
        error: function (xhr, ajaxOptions, thrownError) { 
         alert(xhr.status + ' ' + thrownError); 
        } 
       }); 
      } 
     }); 

PHP:

public function getUnitsHTML() { 
     $jinput = JFactory::getApplication()->input; 
     $db = JFactory::getDbo(); 
     $query = $db->getQuery(true);   
     $sdate = date_create($jinput->get ('dsvalue')); 
     $mystartdate = date_format($sdate, "Y-m-d");   
     echo '<div>'.$mystartdate.'</div></br>';    
     } 

回答

0

要绑定2个改变事件的#calendar。相反,尝试这样的事:

function yourCalculation(start_val) { 
    execute_whatever_you_want; 
}; 
$('#calendar').change(function() { 
    value = $(this).val() 
    yourCalculation(value); 

    var selectvalueds = value; 
    Unit_id.html('<option value="">Loading...</option>'); 
    if (selectvalueds == '') { 
     Unit_id.html(initial_Unit_html); 
    } else { 
     $.ajax({ 
       url: 'index.php', 
       data:'option=com_myapp&task=getUnitsHTML&dsvalue='+selectvalueds, 
       success: function(output) { 
        Unit_id.html(output); 
       }, 
       error: function (xhr, ajaxOptions, thrownError) { 
        alert(xhr.status + ' ' + thrownError); 
       } 
     }); 
    } 
}); 

这将首先获得的价值,执行你的计算功能,然后添加加载选项,检查值不为空,如果它不为空,启动Ajax调用。