Cakephp+;Jquery:如何使用Jquery方法?
我的问题很简单 我有很多链接,比如:Cakephp+;Jquery:如何使用Jquery方法?,php,jquery,cakephp,Php,Jquery,Cakephp,我的问题很简单 我有很多链接,比如: $this->Html->link(__('Calculer'), 'log_import/'.$suivi['Suivi']['date'], array('class' => 'logImport') 如您所见,我的“a”标签的href属性在每个链接上都会发生变化(类似于“2012-01-01”、“2012-10-15”等日期) 当我单击我的链接时,jquery代码正在执行: $this->Js ->get('.l
$this->Html->link(__('Calculer'), 'log_import/'.$suivi['Suivi']['date'], array('class' => 'logImport')
如您所见,我的“a”标签的href属性在每个链接上都会发生变化(类似于“2012-01-01”、“2012-10-15”等日期)
当我单击我的链接时,jquery代码正在执行:
$this->Js
->get('.logImport')
->event('click',
$this->Js->request(
array('action' => '$(this).attr("href")'),
array('async' => true,
'update' => '#test')
));
我得到一个错误,因为ajax请求使用的URL是:
/suivis/$(this).attr("href")
而不是:
/suivis/log_import/2012-01-01
我认为这行不通:
array('action' => '$(this).attr("href")')
该值似乎被视为字符串
我的建议是使用原始jquery代码:
$('.logImport',this.DOMDoc).each(
function(index, object){
var href = $(this).get(0).href;
$(this).get(0).href = "javascript:void(0);"
// attach click function for each link
$(this).click(function() {
// make request here
$.ajax({
type: "post",
url: href,
...
})
});
}
);
是的,最后我在JS助手缓冲区中使用了“纯”jquery代码
这是我的代码(您可以直接使用$(This).attr并绕过each循环;-)
顺便说一下,如果有人知道的话,我仍然对答案感兴趣……你为什么不使用JsHelpers链接方法?我所说的“纯”是指不要将PHP与Javascript混用。Js帮助器在某些情况下可能很有用,但是如果您只是使用它来显示脚本,那么它就变得不必要了。
$this->Js->buffer('
$(".logImport").click(function(event) {
event.preventDefault();
$.ajax({
url: $(this).attr("href"),
})
.done(function(){
alert("OK");
})
.fail(function(){
alert("FAILED");
});
});
');