jQuery UI选项卡:如何使用发布数据发送ajax请求?

Vit*_*mar 4 jquery jquery-ui jquery-ui-tabs

jQuery UI选项卡:

<script>
$(function() {
    $( "#tabs" ).tabs({
        ajaxOptions: {
            error: function( xhr, status, index, anchor ) {
                $( anchor.hash ).html("Couldn't load this tab.");
            }
        }
    });
});
</script>


<div class="demo">

<div id="tabs">
    <ul>
        <li><a href="#tabs-1">Preloaded</a></li>
        <li><a href="ajax/content1.html">Tab 1</a></li>
        <li><a href="ajax/content2.html">Tab 2</a></li>
    </ul>
    <div id="tabs-1">
        <p>Proin elit arcu, rutrum commodo.</p>
</div>
Run Code Online (Sandbox Code Playgroud)

在这种情况下如何发送带有帖子数据的ajax请求(可能通过ajaxOptions).

我不知道如何修改标签网址以发送帖子数据,例如:

<li><a href="ajax/content1.html(country:1,city:35)">Tab 1</a></li>
<li><a href="ajax/content2.html(code:'aa')">Tab 1</a></li>
Run Code Online (Sandbox Code Playgroud)

谢谢!

编辑:

在jQuery中它是:

$.load("some_url",{country: countryValue});
Run Code Online (Sandbox Code Playgroud)

所以我有帖子标题(国家)和帖子值(countryValue).如何对每个标签做同样的事情?

Cᴏʀ*_*ᴏʀʏ 5

要使AJAX方法POST,您可以typeajaxOptions对象添加.要收集帖子的数据,您可以利用jQuery.data()然后隐藏锚点中的POST参数.

<div id="tabs">
    <ul>
        <li><a href="#tabs-1">Preloaded</a></li>
        <li><a href="ajax/content1.html">Tab 1</a></li>
        <li><a href="ajax/content2.html" data-country="1" data-city="35">Tab 2</a></li>
        <li><a href="ajax/content3-slow.php">Tab 3 (slow)</a></li>
        <li><a href="ajax/content4-broken.php">Tab 4 (broken)</a></li>
    </ul>
    <div id="tabs-1">
        <p>Proin elit arcu, rutrum commodo.</p>
</div>
Run Code Online (Sandbox Code Playgroud)

和JavaScript:

var postData = {};

$("#tabs").tabs({
    select: function(event, ui) {
        postData = {
            country: parseInt($(ui.tab).data('country')),
            city: parseInt($(ui.tab).data('city'));
        };
    },
    ajaxOptions: {
        type: 'POST',
        data: postData,
        error: function(xhr, status, index, anchor) {
            $(anchor.hash).html("Couldn't load this tab. We'll try to fix this as soon as possible. " + "If this wouldn't be a demo.");
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

试试吧:JSFiddle

如果您的参数对每个链接都有所改变,那么您必须想出一种方法来了解您正在寻找的参数.您可以使用select()事件获取选项卡的索引,ui.index并使用a switch为每个案例获取不同的参数.不可否认,这个解决方案不是很漂亮,但它可以工作.