我正在使用PHP腳本來生成一些包含javascript(來自jQuery UI的DatePicker)的HTML。在AJAX響應中的jQuery UI日期選取器
使用jQuery/AJAX從主頁面調用PHP腳本。我所有的HTML渲染的沒有問題,但我得到的是說,一個控制檯錯誤:
遺漏的類型錯誤:$(...)日期選擇器是不是一個函數
我清楚自己做錯事,但沒有太大的一位Javascript專家。代碼工作正常,如果我直接調用new-fields.php,但是當我通過jQuery獲取它時會中斷。感謝任何幫助!
main.html中:
<script type="text/javascript">
jQuery('input[name="location"]').click(function(){
var data = {location : jQuery(this).val(), department : $('input[name="department"]:checked').val()};
jQuery.ajax({
url: "/new-fields.php",
type:'POST',
data: data,
dataType: 'text',
success: function(result){
jQuery('#div-custom').html(result).show();
$("#div-custom").find("script").each(function() {
eval($(this).text());
});
}
});
});
</script>
新fields.php:
$picker = '<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(function() {
$("#datepicker1").datepicker({
numberOfMonths: 3,
showButtonPanel: true
});
});
</script>
<script>
$(function() {
$("#datepicker2").datepicker({
numberOfMonths: 3,
showButtonPanel: true
});
});
</script>
<p>Date: <input type="text" id="datepicker1"></p>
<p>Date: <input type="text" id="datepicker2"></p>';
echo $picker;
我不是不清楚如何main.html中和新fields.php結合在一起。哪一個產生錯誤?另外(假設這是你的整個代碼)把你的HTML字符串存儲在一個類似PHP的變量中是毫無意義的,然後將它回顯給瀏覽器。相反,用'?>'結束你的PHP標記,然後直接輸出你的HTML。 – Mike
只是一個猜測,但因爲你在調用datepicker之前包括查詢ui ..也許它是在庫加載完成之前調用函數,因此它不可用於運行? – TinMonkey