通过 Ajax 传递日期值
Passing date value by Ajax
我有两个通过 Ajax 传递我的日历值。问题是,日期值仅在 selecting 日期的第二次尝试后传递,并且它正在传递第一次尝试值!
以当前结果为例:
第一次尝试我会 select: 2017-03-01
selectvalueds = ‘ ‘
第二次我会 select: 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>';
}
您正在将 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调用。
我有两个通过 Ajax 传递我的日历值。问题是,日期值仅在 selecting 日期的第二次尝试后传递,并且它正在传递第一次尝试值!
以当前结果为例:
第一次尝试我会 select: 2017-03-01
selectvalueds = ‘ ‘
第二次我会 select: 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>';
}
您正在将 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调用。