开发者

Ajax update parameter

I'm using jquery ajax to fetch a page called matid.jsp like this::

var loadUrl = "matid.jsp";


          $(".get").click(function () {
              $.get(loadUrl, function (responseText) {
                  $("#result").html(responseText);
              });
          });

Is there a way to开发者_运维问答 update this var loadurl from the body of the html page to look like this

"matid.jsp?hello" the next time a click is made?

Below is how I create multiple divs that should have different loadurl parameter when the .get() function is called

for(int i=1; 1<20; i++){
<div onclick="$.get()" class="get"><%=i%> </div>
}

Thanks


Umm, you can add data attributes on your div, these were introduced in HTML5. You can use jQuery to update/change the data-url attribute inside the click function if you need now. Hope this helps. the JavaScript

$(".get").click(function(){

   // get the page associated with the div element
   // the below line gets the value of the data-url attribute
   var page = jQuery.data($(this), "url");

   // load the page with a GET request
   $.get(page, function(responseText){
      // set the result
      $(".result").html(responseText);
   });
});

the HTML

<div class="get" data-url="somepage.php?page=1"></div>
<div class="get" data-url="somepage.php?page=2"></div>

<div class="result">
</div>

For your reference on data attributes:

In HTML: http://ejohn.org/blog/html-5-data-attributes/

In jQuery: http://api.jquery.com/jQuery.data/

Good luck.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜