weixin_33708432 2017-04-02 15:48 采纳率: 0%
浏览 383

通过Ajax传递日期值

I have two pass my calendar value through Ajax. The problem is, date value passing only after second try of selecting date and it is passing the first try value!!

The current result as an Example:

On first try I will select: 2017-03-01

selectvalueds = ‘ ‘

On second try I will select: 2015-05-30

Selectvalueds = ‘2017-03-01’

Important:! Due to some calculation i have to first copy the value of calendar to #start and then pass #start through Ajax to my php code. 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>';            
        }
  • 写回答

1条回答 默认 最新

  • csdnceshi62 2017-04-02 18:59
    关注

    You are binding 2 change events to the #calendar. Instead, try something like this:

    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);
                  }
           });
        }
     });
    

    This will first get the value, execute your calculation function, then add the Loading option, check if the value is not empty and if it's not empty, start Ajax call.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测