最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery tabs: change ajax data on tab click - Stack Overflow

programmeradmin0浏览0评论

I have used jquery tabs for loading data with ajax, but I need to add some parameters to the url when the user clicks in a tab. I don't know the parameters in advance because they e from a form which has been filled by the user. So I've tried something like the following code, but I don't get it working:

<div id="tabs">
    <ul>
        <li><a href="${tab1_url}">Tab 1</a></li>
        <li><a href="${tab2_url}">Tab 2</a></li>
        <li><a href="${tab3_url}">Tab 3</a></li>
    </ul>
</div>

and I serialize the form in an array and join the array to the array containing the satic data.

var staticData = [{name:'id',value:'${myId}'}];
$("#tabs").tabs({
    var $tabs = $("#tabs").tabs({
        ajaxOptions: { data: staticData},
        select: function(event, ui) {
            var dynamicData = $("#mon_form").serializeArray();
            var dataToSend = staticData.concat(dynamicData);
            $("#tabs").tabs("option", "ajaxOptions", { 'data': dataToSend });
             return true;
        }
    });
});

but this doesn't update the ajax data after the tabs are created (I'm seeing the request sent with Firebug and it only includes the initial params).

How can I change the ajax data when the user clicks the tab?

Thanks

EDITED: now with this code works

I have used jquery tabs for loading data with ajax, but I need to add some parameters to the url when the user clicks in a tab. I don't know the parameters in advance because they e from a form which has been filled by the user. So I've tried something like the following code, but I don't get it working:

<div id="tabs">
    <ul>
        <li><a href="${tab1_url}">Tab 1</a></li>
        <li><a href="${tab2_url}">Tab 2</a></li>
        <li><a href="${tab3_url}">Tab 3</a></li>
    </ul>
</div>

and I serialize the form in an array and join the array to the array containing the satic data.

var staticData = [{name:'id',value:'${myId}'}];
$("#tabs").tabs({
    var $tabs = $("#tabs").tabs({
        ajaxOptions: { data: staticData},
        select: function(event, ui) {
            var dynamicData = $("#mon_form").serializeArray();
            var dataToSend = staticData.concat(dynamicData);
            $("#tabs").tabs("option", "ajaxOptions", { 'data': dataToSend });
             return true;
        }
    });
});

but this doesn't update the ajax data after the tabs are created (I'm seeing the request sent with Firebug and it only includes the initial params).

How can I change the ajax data when the user clicks the tab?

Thanks

EDITED: now with this code works

Share Improve this question edited Dec 15, 2010 at 10:01 Javi asked Dec 15, 2010 at 8:44 JaviJavi 19.8k32 gold badges108 silver badges138 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

I think that what you are looking for is the "url" method :

http://jqueryui./demos/tabs/#method-url

You code would look something like that :

$(function(){
    $("#tabs").tabs({
        select: function(event, ui) {
           var data = $("#mon_form").serialize();               
           if(ui.index == 1){
              var url = '${tab2_url}' + "&" + data;
              $("#tabs").tabs("url", 1, url); //this is new !
           }
           return true;
        }
    });
});
jQuery(document).ready(function($){
$("a[name=tab]").click(function(e){
if($(this).attr('id')=="tab1")
{
//pass url1
}
if($(this).attr('id')=="tab2")
{
//pass url2
}
if($(this).attr('id')=="tab3")
{
//pass url3
}

});
});

This work for me but if i need for ex 100 tabs i need to put in all 100 tabs url.

<div id="tabs">
    <ul>
        <li><a href="" name="tab" id="tab1">Tab 1</a></li>
        <li><a href="" name="tab" id="tab2">Tab 2</a></li>
        <li><a href="" name="tab" id="tab3">Tab 3</a></li>
    </ul>
</div>

Jquery code...

jQuery(document).ready(function($){
$("a[name=tab]").click(function(e){
if($(this).attr('id')=="tab1")
{
//pass url1
}
if($(this).attr('id')=="tab2")
{
//pass url2
}
if($(this).attr('id')=="tab3")
{
//pass url3
}

});
});
发布评论

评论列表(0)

  1. 暂无评论