我目前正在努力,因为我想加载由 PHP 提交的相同页面。为了更清楚地说明这一点,这是一个评论部分,在人们提交消息后我想检索相同的信息以及新的信息。
我遇到了问题,因为我不是通过 AJAX 执行此操作。
因此,index.php 具有以下内容:
/* Before HTML tag */
if(Cookie::Exists('comment')){
$data = unserialize(stripslashes(Cookie::Get('comment')));
if($data['status'] == 1){
$page = "<script type='text/javascript'>set_product_id('" . $data['id'] . "')</script>";
}
Cookie::Delete('comment', time() - 3600);
}
/* Further ahead.. (inside body) */
<form method="POST" action="insert_info.php" data-ajax="false">
<input type="hidden" id="product_id" name="product_id"/>
<input type="text" id="name_" name="name_" required="required"/>
<input type="text" id="comment" name="comment" required="required"/>
</form>
/* After body */
<script type="text/javascript">
$(document).on("pagebeforeshow", "#page-comments", function() {
$(function(){
get_comments(objs.product_id);
});
});
var objs = {
product_id : ''
}
function set_product_id(id){
objs.product_id = id;
$.mobile.changePage('#page-comments', {role: 'dialog'});
}
</script>
/* After the HTML tag */
<?php echo $page; ?>
我确实收到了正确的 ID 等,在提交评论之前它也运行得很好。页面加载内容没有任何问题等
插入_info.php
new_comment($host, $user, $pass, $db);
$arr = array("status" => 1, "id" => $_POST['product_id']);
Cookie::set('comment', serialize($arr), time()+60);
header("Location: ../index.php");
exit();
当我执行此操作时,我在 google chrome 控制台上收到以下错误:
Uncaught TypeError: Cannot call method 'trigger' of undefined
指向线:
function set_product_id(id){
objs.product_id = id;
$.mobile.changePage('#page-comments', {role: 'dialog'});
}
我错过了什么?谢谢。
编辑:将 set_product_id() 函数更改为:
$('#page-main').on('pagecreate', function(event) {
$.mobile.changePage('#page-comments', {role: 'dialog'});
});
评论页面出现又消失。但是,如果我删除“对话框”,页面将保留并正常工作。但我需要将其显示在对话框中。
最佳答案
您的$.mobile.changePage()
需要包含一个网址..
所以这个:
$.mobile.changePage('#page-comments', {role: 'dialog'});
应该是这样的:
$.mobile.changePage('../index.php', {role: 'dialog'});
关于javascript - PHP 提交后 jQuery 加载同一页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20688082/