ajax执行成功后如何更改ajax链接的URL参数?以下代码不起作用,但解释了我想要做什么。
CHtml::ajaxLink($text, array('/core/bookmark/create'), array(
'data' => array("id" => $id),
'type' => 'GET',
'success'=>'js:function(data){
if (data.status == "success"){
//attempt to dynamically change the URL
this.url = "/core/bookmark/delete";
$("#bookmark-btn").toggleClass("bookmark_on").toggleClass("bookmark_off");
}
}',
), array('id'=>'bookmark-btn', 'class' => 'bookmark_off'));
带有 this.url
的行对 ajaxLink 的 URL 没有影响,但也不会在控制台中引发任何错误。
最佳答案
CHtml::ajaxLink
呈现 a
元素,该元素没有 url
属性。您需要这样的代码:
if (data.status == "success"){
jQuery(this).attr('href', '/core/bookmark/delete');
}
更新:
Besides, what I'm wanting to do is change the URL in the Ajax function which fires when the link is clicked, not on the link itself.
在这种情况下,更好的解决方案是绘制两个 CHtml::ajaxLink
元素并根据 ajax 请求结果切换它们的可见性。这将花费您更少的精力。
关于php - 成功时动态更改 Yii ajaxLink 的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13345619/