【问题标题】:Load AJAX content in modal and change URL以模式加载 AJAX 内容并更改 URL
【发布时间】:2015-05-07 14:44:10
【问题描述】:
我当前的设置是单击一个模式弹出窗口,其中包含来自已传递 id 的 ajax 操作的数据,我希望 URL 在单击时更改。
但我也想要它,这样如果你直接访问 URL,它会加载预加载模式的索引。
与https://www.myunidays.com/perks/view/shoeaholics/online 非常相似,它会在模型中加载带有内容的 URL,然后如果您单击/关闭模式,则 URL 会更改为索引页面。
我已经看到有关在点击时更改 URL 的相关问题,但找不到与直接访问 URL 相关的任何问题(这是我可以添加到我的 .htaccess 中的规则)。
(任何代码/方向表示赞赏)
【问题讨论】:
标签:
javascript
php
ajax
url
【解决方案1】:
创建一个局部视图(这样布局不会被渲染两次)并将动作添加到控制器
public function actionViewmodal($id)
{
return $this->renderPartial('_view', array('model' => $this->findModel($id)));
}
然后在我的索引中我做了以下操作
$(document).ready(function() {
$('a').click(function() {
pageurl = $(this).attr('href');
var Id = jQuery(this).attr('id');
$.ajax({
type: 'POST',
data: {
'modal_id' : Id,
},
url : 'http://localhost:8888/directory/viewmodal?id='+Id,
success: function(response) {
if(response) {
$('.modal_some_wrapper').html(response);
$('#modal_'+Id).modal('show');
$(document).on('hidden.bs.modal', modal_id, function (event) {
$(this).remove();
});
} else {
alert('Error');
}
}
});
//to change the browser URL to the given link location
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
//stop refreshing to the page given in
return false;
});
});
有人可以举例说明 myunidays 网站如何处理模式和 URL 更改吗?