如何在没有任何事件的情况下从 document.ready 上的服务器端检索值
How to retrieve the value from server side on document.ready in jquery without any event
我有一个要求,我需要从任何事件的服务器端代码中检索值,并使用它来制作对话框。我试图在window.onload中获取值,但我知道window.onload是在document.ready函数之后调用的,而我希望在document.ready函数中调用该值。
我担心的是,我可以通过任何方法执行 ajax 调用以从服务器端获取值并使用它,而无需在 document.ready 函数上执行任何手动事件。
这是我的窗口加载代码。
$(window).load(function() {
$.ajax({
type: 'GET',
url: 'Sites',
dataType: "text",
success: function(data) {
var values = [];
values = data;
values=values.replace('[','');
values=values.replace(']','');
var array = values.split(",");
for (var i in array){
output =output.concat('<input type="checkbox" id="'+array[i]+'" name="'+array[i]+'" value="'+array[i]+'" />'+array[i]+'<br />');
}
console.log(output);
alert(output);
}
});
});
这是我的文档.ready函数代码。
$(document).ready(function() {
//var windowWidth = (document.documentElement.clientWidth - 100) /0.9;
var chbx="";
chbx=output.toString();
alert(chbx);
var $dialog = $('<div></div>').html("<form id='myform'>" +output + "</form>")
.dialog({
autoOpen: false,
title: 'Select Sites',
buttons: {
"Submit": function() { $('form#myform').submit();},
"Cancel": function() {$(this).dialog("close");}
}
});
在这里,我正在尝试访问变量输出i document.raedy函数,我需要从window.load获取。但正如问题所述,它总是空的
请伙计们帮助我..我在这个领域是新手。提前致谢//
我将 ajax 调用代码放在对话框代码之前,并在$.ajax
中添加了一个选项async:false
,以便它等到从服务器收到响应。因此,请尝试以下代码:
$(document).ready(function() {
//call the ajax first
$.ajax({
type: 'GET',
url: 'Sites',
dataType: "text",
async:false,//wait till the data is received from the server
success: function(data) {
var values = [];
values = data;
values=values.replace('[','');
values=values.replace(']','');
var array = values.split(",");
for (var i in array){
output =output.concat('<input type="checkbox" id="'+array[i]+'" name="'+array[i]+'" value="'+array[i]+'" />'+array[i]+'<br />');
}
console.log(output);
alert(output);
}
});
///then here comes you code for dialog
var chbx="";
chbx=output.toString();
alert(chbx);
var $dialog = $('<div></div>').html("<form id='myform'>" +output + "</form>")
.dialog({
autoOpen: false,
title: 'Select Sites',
buttons: {
"Submit": function() { $('form#myform').submit();},
"Cancel": function() {$(this).dialog("close");}
}
});
});
假设您对服务器的 AJAX 调用是正确的并给出响应(OP 的问题中缺少服务器端代码) 您应该将 ajax 调用移出 window.load 并在执行任何其他函数之前对其进行预置,这样您就可以使用服务器生成的输出使用 JS(为什么不在服务器端这样做呢?
无论如何 - 这是您的代码的示例更新,注意未测试。
//Set output variable so that it's accessible for other functions.
var output = '';
$(document).ready(function() {
//Do ajax initially.
//You don't have to wait until the page finished loading all the images.
//document.ready only loads the DOM, which is needed for this to work.
$.ajax({
type: 'GET',
url: 'Sites',
dataType: "text",
success: function(data) {
var values = [];
values = data;
values=values.replace('[','');
values=values.replace(']','');
var array = values.split(",");
for (var i in array){
output =output.concat('<input type="checkbox" id="'+array[i]+'" name="'+array[i]+'" value="'+array[i]+'" />'+array[i]+'<br />');
}
//Do all the further processing on success of the ajax call.
//var windowWidth = (document.documentElement.clientWidth - 100) /0.9;
var chbx="";
chbx=output.toString();
alert(chbx);
var $dialog = $('<div></div>').html("<form id='myform'>" +output + "</form>")
.dialog({
autoOpen: false,
title: 'Select Sites',
buttons: {
"Submit": function() { $('form#myform').submit();},
"Cancel": function() {$(this).dialog("close");}
}
});
console.log(output);
alert(output);
}
});
});
根据下面的评论进行编辑,
在 AJAX 之后执行的代码实际上应该在success: function();
回调中。我已经编辑了上面的代码以保持答案小。
相关文章:
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- jQuery document.ready not working
- jQuery document.ready停止代码
- 停止对document.ready函数的重定向/刷新
- $(document).ready和jQuery(function($)不工作;jQuery已定义,但脚本尚未定义;t进
- 函数从$(document).ready外部调用在$(document).ready中定义的函数
- 如果没有document.ready(),我怎么能拥有多个javascript函数呢
- ASPX Jquery 1.11 $(Document).ready(function() {} was skipped
- 重载像$(document).ready这样的函数是错误的
- 延迟加载jquery后替换$(document).ready(function)
- $(document).ready未在brunch/jade/主干网项目中调用
- 为什么不'加载$(document.ready(function)后,单击“工作”
- $(document)中的多个javascript函数.ready不工作
- JavaScript执行从函数声明开始,而不是从$(document).ready()开始
- 从Document.ready中的bundle执行javascript
- 多个 $(document).ready(function() 但只有前几个被解雇
- 如何在 $(document).ready(function(){}) 中使用 for 循环
- 从 document.ready 调用 jQuery 插件抛出 getPreventDefault 错误
- 为什么一个 $(document).ready(function() { 会阻止另一个运行
- 调用 Document.ready 外部的 AJAX 函数