简体   繁体   English

Symfony和jQuery.data()

[英]Symfony and jQuery.data()

I have an ajax navigation system in my app. 我的应用程序中有一个ajax导航系统。 Each ajax link is of class ajax and the page it needs to load is saved in an aurl attribute, for example: 每个ajax链接均属于ajax类,需要加载的页面保存在aurl属性中,例如:

<a class="ajax" aurl="/user/posts/3/edit">Edit Post</a>

The aurl is given to the element using symfony's url_for method in the server-side. 在服务器端使用symfony的url_for方法将aurl赋予元素。

I thought about more elegant way of saving the element's aurl attribute, like jQuery.data() , but how can i use it from the server side? 我想到了一种保存元素的aurl属性(如jQuery.data()更优雅的方法,但是如何从服务器端使用它呢? creating a script after each element to set it's aurl doesn't seem like a good solution.. 在每个要设置其aurl元素之后创建脚本似乎不是一个好的解决方案。

Any ideas? 有任何想法吗?

<a class="ajax" data-aurl="/user/posts/3/edit">Edit Post</a>

Then you can access it through .data('aurl') if you use a recent jQuery version (1.5 or newer AFAIK). 然后,如果您使用最新的jQuery版本(1.5或更高版本的AFAIK),则可以通过.data('aurl')访问它。

However, why don't you simply set the href (which should be set anyway) and then use $(this).attr('href') to get the url and use e.preventDefault(); 但是,为什么不简单地设置href (无论如何应该设置),然后使用$(this).attr('href')获取url并使用e.preventDefault(); (with e being the first argument of the click handler function) to prevent the link from loading without AJAX? (以e作为点击处理程序函数的第一个参数)以防止在没有AJAX的情况下加载链接?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM