jQuery 加载到变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6203502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery load to variable
提问by honzahommer
I need insert result from jQuery load to variable.
我需要将 jQuery 加载的结果插入到变量中。
Content of elemtent with id test on page ajax.html.
ajax.html 页面上带有 id 测试的元素内容。
$('#result').load('ajax.html #test');
采纳答案by MarioRicalde
$(document).ready(function(){
$('#result').load('/ p#name', function(result) {
var obj = $(this).find('p#name'), html = obj.html();
obj.css({'font-size':40});
$(this).append($('<div>').text(html));
});
});
Example on JSFiddle http://jsfiddle.net/kurtheitroad/stD94/
JSFiddle 示例http://jsfiddle.net/kurtheitroad/stD94/
回答by Daniel Baulig
Try using jQuery.get
instead.
尝试使用jQuery.get
。
Eg.
例如。
$.get('ajax.html', function (data) {
data = $(data).find('#test');
// data contains your html
});
回答by Darin Dimitrov
$('#result').load('ajax.html #test', function(result) {
var variable = $('#result').html();
});
回答by Tsonev
More or less doing the same as above but simpler:
或多或少与上述相同但更简单:
var dt;
$.get("ajax.html", function (data) {
dt = data;
// data contains your html
});
回答by CityPickle
I'm posting this message on all the stackoverflow threads that ask for help in cramming data from a jQuery ajax method into a variable, rather than into an HTML element, because I had a tough time finding a solution that would work. I finally found a solution, and I just want to share with anyone who is having a hard time with this.
我在所有寻求帮助将数据从 jQuery ajax 方法填充到变量而不是 HTML 元素的 stackoverflow 线程上发布此消息,因为我很难找到可行的解决方案。我终于找到了解决方案,我只想与遇到此问题的任何人分享。
My problem was that I was really struggling with getting the results of jQuery ajax into my variables at the "document.ready" stage of events.
我的问题是我真的很难在事件的“document.ready”阶段将 jQuery ajax 的结果放入我的变量中。
My tests showed me that jQuery's ajax would load into my variables when a user triggered an "onchange" event of a select box after the page had already loaded, but the data would not feed the variables when the page first loaded. But I didn't want to trigger any ajax on my "onchange" event; I wanted the variables, fed with ajax data on page load, to handle the work. The variablesare used to change data on the fly when the user triggers an "onchange" event. This way there is no lag/ajax while the user is interacting with the form; the data is available once the page loads.
我的测试表明,当用户在页面加载后触发选择框的“onchange”事件时,jQuery 的 ajax 会加载到我的变量中,但在页面首次加载时数据不会提供变量。但我不想在我的“onchange”事件上触发任何 ajax;我想要在页面加载时提供 ajax 数据的变量来处理工作。当用户触发“onchange”事件时,这些变量用于动态更改数据。这样在用户与表单交互时就没有延迟/ajax;一旦页面加载,数据就可用。
I tried many, many, many different methods, but in the end, the code I needed was on this stackoverflow thread : JQuery - Storing ajax response into global variable
我尝试了很多很多不同的方法,但最后,我需要的代码在这个 stackoverflow 线程上:JQuery - 将 ajax 响应存储到全局变量中
I used Charles Guilbert's response. Using his answer, I am able to get data into my variables, even when my page first loads.
我使用了查尔斯吉尔伯特的回应。使用他的回答,即使我的页面第一次加载,我也可以将数据放入我的变量中。
Here's an example of the working script - hope it helps someone!
这是工作脚本的示例 - 希望对某人有所帮助!
jQuery.extend
(
{
getValues: function(url)
{
var result = null;
$.ajax(
{
url: url,
type: 'get',
dataType: 'html',
async: false,
cache: false,
success: function(data)
{
result = data;
}
}
);
return result;
}
}
);
// Option List 1, when "Cats" is selected elsewhere
optList1_Cats += $.getValues("/MyData.aspx?iListNum=1&sVal=cats");
// Option List 1, when "Dogs" is selected elsewhere
optList1_Dogs += $.getValues("/MyData.aspx?iListNum=1&sVal=dogs");
// Option List 2, when "Cats" is selected elsewhere
optList2_Cats += $.getValues("/MyData.aspx?iListNum=2&sVal=cats");
// Option List 2, when "Dogs" is selected elsewhere
optList2_Dogs += $.getValues("/MyData.aspx?iListNum=2&sVal=dogs");
回答by Taras Pukhliar
If you want exactly load func, try this one:
如果你想要完全加载 func,试试这个:
var temp;
$(new Object()).load('YourUrl', { param: "smth" }, function(data){
$('#element').prepend(data); //if you want prepend/append result to some html
temp = data; //if you want put to variable
});
回答by danish
$('#submit').click(function(){ var taval = $("#message").val();
$("#showcomment").load('comments.php', {commentval:taval}) });
回答by Vitalicus
Use setTimeout
用 setTimeout
<script>
my_var=$('#hidden').load('new.html');
setTimeout(function(){
my_var=my_var.html();
alert(my_var);
}, 500);
</script>