Jquery 页面加载时如何使用日期选择器
我有一个jquery函数,它使用Jquery 页面加载时如何使用日期选择器,jquery,jquery-ui,datepicker,Jquery,Jquery Ui,Datepicker,我有一个jquery函数,它使用load和preventDefault。下面是我的main.php页面中的代码 <script> $(document).ready(function() { function ajaxLoad(e) { e.preventDefault(); var page = $(this).attr('href'); $('#content').load(page, function(){ $("#d
load
和preventDefault
。下面是我的main.php
页面中的代码
<script>
$(document).ready(function() {
function ajaxLoad(e) {
e.preventDefault();
var page = $(this).attr('href');
$('#content').load(page, function(){
$("#datepicker").datepicker();
});
$(document).on("click", "a#staff_insert, ajaxLoad);
});
</script>
我想知道如何使用datepicker功能,因为当我单击输入
字段时,日历不会出现。我必须在staff add.php
上插入datepicker函数吗
$(document).ready(function(){
$("#datepicker").datepicker();
});
谢谢你的帮助
使用新代码更新。我只是想告诉你我成功了但现在问题是 当我点击不同的链接,然后点击返回到有 日期选择器,日期选择器将不会出现。只有当我 没有转到其他链接。我能知道我做错了什么吗
$(文档).ready(函数(){
函数ajaxLoad(e){
e、 预防默认值();
if($(this.attr('id')=='staff\u insert'){
var page=$(this.attr('href');
$(“#内容”).load(第+页“#日历”,函数(){
$(“#日期选择器”).datepicker();
});
}否则{
var page=$(this.attr('href');
$('#content')。加载(第页);
}
}
$(文档)。在(“单击”,“员工信息,员工插入”,ajaxLoad);
});
如果我在浏览器上看到,错误是uncaughttypeerror:$(…)。datepicker不是函数
更新-已解决
我可以通过插入
var j=jQuery.noConflict()来解决这个问题代码>上的代码。有人能解释一下我为什么需要这个吗?仍在寻找原因。谢谢因为您是在本地开发的,所以我认为您使用的是文件协议(如file://)
当前,您正在引用脚本/css include with the protocol:
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
在本例中,使用相对URI,基本上它允许浏览器根据页面使用的协议确定是使用http://还是https://
您正在使用文件协议,因此页面尝试加载:
<link rel="stylesheet" href="file://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="file://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
它将不起作用,因此将协议设置为:
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
你确定没有多个#datepicker元素?我打赌(除了@void的评论)在AJAX调用后,您必须重新初始化datepicker。document ready函数只启动一次……如果您通过AJAX加载html,它将不会再次启动document ready函数……是否有其他方法或最佳方法来执行此操作?您确定包含jquery ui文件吗?谢谢帮助。我试图更改协议,但仍然出现相同的错误
<link rel="stylesheet" href="file://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="file://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>