jQuery UI ajax 上的日期选择器日期格式无效
jQuery UI datepicker dateformat on ajax didn't work
我遇到了奇怪的问题。我想要 jQuery UI DatePicker 和日期格式的下拉列表。
我找到了 this,并且在页面加载时进行了测试并且工作正常。但是,当我通过 ajax 调用加载页面时,相同的代码不起作用。
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
$( function() {
$( "#id_management_date" ).datepicker();
$( "#id_management_date_format" ).on( "change", function() {
alert(3);
$( "#id_management_date" ).datepicker( "option", "dateFormat", $( this ).val() );
});
} );
</script>
加载 ajax 内容后,我可以 select 日历。但是当我更改格式下拉列表时,它不是在日期选择器中格式化日期。
这是因为该元素是在页面加载后动态加载的,而不是原始元素的一部分 DOM。
像这样引用它
$("body").on("change", "#id_management_date_format", function(){
alert(3);
$( "#id_management_date" ).datepicker( "option", "dateFormat", $( this ).val() );
});
编辑: 在 运行 本地测试之后,它是同一个问题,但是在日期选择器输入初始化期间,在它们被添加到 DOM.
示例工作伪代码。
HTML + 脚本
<!DOCTYPE>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
</head>
<body>
<header></header>
<div id="holder">
<input type="button" onclick="GetDatePickers();" value="Get Date Picker Inputs"/>
</div>
<footer></footer>
<script>
function GetDatePickers(){
$.ajax({
type:"post",
url:"getdatepickers.php",
data:{test:"test"},
success:function(response){
console.log(response);
$("#holder").append(response);
$( "#id_management_date" ).datepicker();
$( "#id_management_date_format" ).datepicker("option", "dateFormat");
},
error:function(resposne){
console.log(response);
}
});
};
$(function() {
$( "body" ).on( "change", "#id_management_date_format", function() {
alert(3);
console.log($(this).val());
$( "#id_management_date" ).datepicker( "option", "dateFormat", $(this).val());
});
});
</script>
</body>
</html>
PHP - 要发送的文件 Ajax 呼叫
if(isset($_POST['test'])){
$html = <<<DOC
<input type="text" id="id_management_date"/>
<input type="text" id="id_management_date_format"/>
DOC;
echo $html;
}
我遇到了奇怪的问题。我想要 jQuery UI DatePicker 和日期格式的下拉列表。
我找到了 this,并且在页面加载时进行了测试并且工作正常。但是,当我通过 ajax 调用加载页面时,相同的代码不起作用。
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
$( function() {
$( "#id_management_date" ).datepicker();
$( "#id_management_date_format" ).on( "change", function() {
alert(3);
$( "#id_management_date" ).datepicker( "option", "dateFormat", $( this ).val() );
});
} );
</script>
加载 ajax 内容后,我可以 select 日历。但是当我更改格式下拉列表时,它不是在日期选择器中格式化日期。
这是因为该元素是在页面加载后动态加载的,而不是原始元素的一部分 DOM。
像这样引用它
$("body").on("change", "#id_management_date_format", function(){
alert(3);
$( "#id_management_date" ).datepicker( "option", "dateFormat", $( this ).val() );
});
编辑: 在 运行 本地测试之后,它是同一个问题,但是在日期选择器输入初始化期间,在它们被添加到 DOM.
示例工作伪代码。
HTML + 脚本
<!DOCTYPE>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
</head>
<body>
<header></header>
<div id="holder">
<input type="button" onclick="GetDatePickers();" value="Get Date Picker Inputs"/>
</div>
<footer></footer>
<script>
function GetDatePickers(){
$.ajax({
type:"post",
url:"getdatepickers.php",
data:{test:"test"},
success:function(response){
console.log(response);
$("#holder").append(response);
$( "#id_management_date" ).datepicker();
$( "#id_management_date_format" ).datepicker("option", "dateFormat");
},
error:function(resposne){
console.log(response);
}
});
};
$(function() {
$( "body" ).on( "change", "#id_management_date_format", function() {
alert(3);
console.log($(this).val());
$( "#id_management_date" ).datepicker( "option", "dateFormat", $(this).val());
});
});
</script>
</body>
</html>
PHP - 要发送的文件 Ajax 呼叫
if(isset($_POST['test'])){
$html = <<<DOC
<input type="text" id="id_management_date"/>
<input type="text" id="id_management_date_format"/>
DOC;
echo $html;
}