通过带有Jquery标签的Ajax传递变量 [英] Passing variable via ajax with Jquery Tabs

查看:71
本文介绍了通过带有Jquery标签的Ajax传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了困扰了我好几天的问题,但我似乎无法解决. 我正在尝试使用Jquery标签通过AJAX传递变量.

I am having a problem that I've been stuck on for days, and I just cant seem to figure it out. I'm trying to pass a variable through AJAX using Jquery Tabs.

这是我的使用场景:用户使用JQuery标签加载页面,默认只是一些文本.在页面上,我有一个包含用户ID的会话变量.当他们单击第二个选项卡时,它将那个userid变量传递给脚本.我无法通过!

Here is my use scenario: User loads page with JQuery tabs, default just being some text. On the page, I have a session variable containing there userid. When they click the 2nd tab, it passes that userid variable to the script. I can't get it to pass!

<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="/js/jquery-ui-1.8.23.custom.min.js"></script>
<link rel="stylesheet" href="js/css/smoothness/jquery-ui-1.8.23.custom.css" />
<script>
var postData = {};
$("#tabs").tabs({
select: function(event, ui) {
    postData = {
        userid: parseInt($(ui.tab).data('userid'));
    };
},
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.");
    }
}
});
</script>

<div id="tabs">
<ul>
    <li><a href="#tabs-1">Intro</a></li>
    <li><a href="Custom/div_charts_test2.html" data-userid="1234">Department</a></li>
</ul>
<div id="tabs-1">
    <p>Text information goes here</p>
</div>
</div>

推荐答案

更改您的select函数以设置ajaxOptions.

Change your select function to set the ajaxOptions.

select:function(event, ui) {
    $("#tabs").tabs("option", "ajaxOptions",{
        type: 'POST',
        data: {userId:$(ui.tab).data('userid')},
        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.");
        }
    })
},

最初定义ajaxOptions时,它将此值用于任何后续请求.但是由于某种原因,它无法获得当前值.我什至尝试仅更改select上的userId,但无法对其进行修改.我不确定他们可能正在克隆价值.无论如何,如果您在请求之前设置ajaxOptions,它将发送您想要的内容.

When you define ajaxOptions initially it uses this value for any subsequent request. But for some reason its not getting the current value. I even tried changing just the userId on select and it failed to modify it. They may be cloning the value, im not sure. Anyway if you set ajaxOptions before the request, it will send what you want.

对不起,我半信半疑,但我不能接受.在问题上+1.我必须做一些研究才能弄清楚为什么它不起作用.

Sorry for the half baked answer, but I couldn't let it go. +1 on the question. I had to do a little research to figure out why it wasn't working.

注意:您可能已经知道这一点,但是必须在服务器上运行才能进行POST. POST到本地文件系统将生成错误.

Note: You probably already know this, but you must be running on a server to POST. POST to the local file system will generate an error.

这篇关于通过带有Jquery标签的Ajax传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆