函数中的jquery变量,如何处理全局变量?(jquery variables in functions, how to handle globals?)

编程入门 行业动态 更新时间:2024-10-15 22:31:31
函数中的jquery变量,如何处理全局变量?(jquery variables in functions, how to handle globals?)

我在jquery中有一个ajax调用,它返回4个不同的数组。 我希望在ajax成功函数之外使用其中3个数组,并且对于某些单击事件,数组中的数据应该是可访问的。

我只是不知道处理数组变量的最佳方法。 有人说走向全球是一个坏主意,其他人说它没关系......所以

ajax成功函数:用其中一个数组做东西

array1,array2需要被其他函数和click事件使用

function display_results_1(){ $('#myDiv').html(array1.id); } $('#binfo').click(function(){ $('#client_info_div').dialog({ $('#myDiv').html(array2.id); }); })

这是我的ajax电话:

$('#c_search').submit(function(){ data = ($(this).serialize()); $.ajax({ url: 'actions/get_company.php', type: 'POST', data: data, cache: false, dataType: 'json', success: function(selected){ `doing stuff here` }) })

我是否应该使用单独的Ajax调用来获取每个不同函数所需的数据?

I have an ajax call in jquery which returns 4 different arrays. 3 of these arrays i want to use outside of the ajax success function and the data in the arrays should be accessible for certain click events.

I just don't know the best way to handle the array variables. some people say going global is a bad idea and other say its ok.. so

ajax success function: does stuff with one of the arrays

array1, array2 needs to be used by other functions and click events

function display_results_1(){ $('#myDiv').html(array1.id); } $('#binfo').click(function(){ $('#client_info_div').dialog({ $('#myDiv').html(array2.id); }); })

This is my ajax call:

$('#c_search').submit(function(){ data = ($(this).serialize()); $.ajax({ url: 'actions/get_company.php', type: 'POST', data: data, cache: false, dataType: 'json', success: function(selected){ `doing stuff here` }) })

Should I use separate Ajax calls to get the data when needed for each different function?

最满意答案

您绝对不希望变量位于全局范围内,但您可以将所有内容包装在函数中以防止它们位于全局范围内:

$(function() { var array1 = []; var array2 = []; function display_results_1(){ $('#myDiv').html(array1.id); } $('#binfo').click(function(){ $('#client_info_div').dialog({ $('#myDiv').html(array2.id); }); }) $('#c_search').submit(function(){ data = ($(this).serialize()); $.ajax({ url: 'actions/get_company.php', type: 'POST', data: data, cache: false, dataType: 'json', success: function(selected){ `doing stuff here` }) }) });

通过执行此操作,您将确保代码也在页面加载时运行,请注意包装函数的$()。

有关详细信息,请参阅http://api.jquery.com/ready/ 。

与之前的答案相反,走向全球并不是一个好主意。 您最终可能会使用相同的变量来处理多个JavaScript。

You definitely don't want your variables in the global scope, but you could wrap everything in a function to prevent them from being in the global scope:

$(function() { var array1 = []; var array2 = []; function display_results_1(){ $('#myDiv').html(array1.id); } $('#binfo').click(function(){ $('#client_info_div').dialog({ $('#myDiv').html(array2.id); }); }) $('#c_search').submit(function(){ data = ($(this).serialize()); $.ajax({ url: 'actions/get_company.php', type: 'POST', data: data, cache: false, dataType: 'json', success: function(selected){ `doing stuff here` }) }) });

By doing this you'll ensure that the code is also running on page load, notice the $() wrapping the function.

See http://api.jquery.com/ready/ for more info.

Contrary to the previous answers, going global is NOT a good idea. You may end up having multiple pieces of JavaScript using the same variables.

更多推荐

本文发布于:2023-07-27 20:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1295057.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   如何处理   函数   全局变量   jquery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!