开发者

Pass data to a jQuery function on server-side

Given I have:

$("a.clickable").livequery('click', (function (e) {
         var values = $(this).attr('id').split('_');
         $('#' + values[3]).load($(this).attr('href'));
         e.preventDefault();
     }));

You see, I want all link开发者_JS百科s with the class "clickable" to cause an ajax load. The target div however varies, so I´ve simply put this - along with some other data - into the Id´s link: "link_value1_value2_targetDivId".

So I currently split the id to get an array of values to work with. I know about *$(obj).data()*, but I need to pass some stuff on serverside.

Is there a ways to achieve this without this ugly id-string-parsing? ASP.Net/MVC btw.


No You can't do that. All that triggering of the AJAX calls has to be done from the client side, though there are ways where you can push some data to client from the server using reverse AJAX or comet but that is not what you are looking for. For your case, you have to do that ugly logic. But if you don't want to parse it, you can store the id in the element itself as data/custom attribute and use it instead of parsing.


I hope I understand the question - there are several and each is related to its div. After the is clicked the related div is manipulated.

How I would do that. On server side (in aspx file) I would generate javascript code like this:

var mapAIdToDivId = new Array();
<% foreach(Item i in MyDivAndACollection) { %>
  mapAIdToDivId['<%= i.AId %>'] = <%= i.DivId %>;
<% } %>

Then the js handler would be

$("a.clickable").livequery('click', (function (e) {
   $('#' + mapAIdToDivId[$(this).attr('id')]).load($(this).attr('href'));
   e.preventDefault();
}));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜