除非有警报,否则功能无法正常工作 [英] function not working correctly unless there is an alert

查看:46
本文介绍了除非有警报,否则功能无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题我遇到的是以下代码,除非我在代码之前立即发出警报,否则不会更新两个选择框"select1"和"select2".

The problem I am having is the following code will not update the tow select boxes "select1" and "select2", unless I have an alert immediately preceding the code.

背景信息-我在此页面上发布了两个变量"sel1val"和"sel2val",并将它们分别分配给ID为"sel1valtry"和"sel2valtry"的隐藏输入框.我将这些输入框的值分配给函数中的变量,并将它们命名为"sel1val"和"sel2val".如果需要,我可以发布更多代码,但这就是我所谓的弗兰克代码" ...哈哈!因为我是新手,所以它是由许多不同样式的代码组成的.

Background info - I am posting two variables to this page, "sel1val" and "sel2val", and I am assigning them to hidden input boxes with id's "sel1valtry" and "sel2valtry" respectively. I am assigning the values of these input boxes to the variables in the function and have named them "sel1val" and "sel2val". I can post more code if need be, but it is what I call "franken code"...haha! because I am a novice, it is assembled from many different styles of code.

目标-我要实现的目标是基于"sel1val"和"sel2val"的值设置两个选择框.只有在代码之前有警报时,才能获得正确的功能.

Objective - what I am trying to achieve is to set two select boxes based upon the value of "sel1val" and "sel2val". The correct functionality is only obtained when I have an alert prior to the code.

我尝试过的方法-我留下了一些注释掉的代码,以说明我的尝试.我曾经参加过许多论坛,而这些论坛正是我从中得到的.我怀疑该警报会重新加载"页面的javascript部分,但是我对此没有真正的依据.我曾经尝试过文档准备就绪",窗口加载"的变体,甚至尝试延迟某些东西来减慢速度,以防出现时间问题.我尝试过的每种方法都产生了相同的结果,因此只能在出现警报的情况下使用.

Methods I have tried - I have left in some commented out code, to illustrate my attempts. I have been through many forums and that is where I got these ideas from. I suspect that the alert "reloads" the javascript part of the page, but I have no real basis for this. I have tried "document ready", variations of "window load", and even tried slowing things down with a delay, in case it was a timing issue. Each of the methods I have tried have resulted in the same outcome, whereby it only works with an alert.

我的基于Web的项目取得了很大的成功,如果没有像这样的论坛中的宝贵资源,这将是不可能的.我要感谢曾经提供过意见/解决方案的任何人,否则,我将永远无法进步.

I have achieved much success with my web based projects, and this would not have been possible without the invaluable resource in forums such as this one. I would like to thank anyone that has ever provided input/solutions, as without this I would never have been able to progress.

$(document).ready(function(){
//  $(document).ajaxSuccess(function(){
//    alert("AJAX request successfully completed");
//  });
//$(window).load(function()
//window.onload = function()
//$(function ()
//Code goes here
//alert("got here");
//{
var sel1val = $("#sel1valtry").val()
var sel2val = $("#sel2valtry").val()

if (sel2val)
{
//alert("will not work without this alert"+sel1val);    

//$("#select1").delay(2000).val(sel1val);
//$("#select1").val(sel1val);
$("#select1").prop('value',sel1val);
//  var element = document.getElementById('select1');
//    element.value = sel1val;
dochange('selection2', sel1val)
//var element = document.getElementById('select2');
//  element.value = sel2val;
alert("will not work without this alert"+sel2val);  
$("#select2").val(sel2val);
}
//}
});
//}

推荐答案

似乎 dochange 函数正在使用一些异步API(例如AJAX调用),该API将值设置为<成功回调中的code> sel2val 变量.但是由于AJAX是异步的,因此该函数立即返回,而不必等待服务器返回响应.通过发出警报,您正在阻止下一行代码的执行,并留出足够的时间来完成AJAX调用并将值分配给 sel2val 变量.

It seems like the dochange function is using some asynchronous API (like an AJAX call for example) which is setting value to the sel2val variable in its success callback. But since AJAX is asynchronous, this function returns immediately, not waiting for the server to return a response. By putting an alert you are blocking the execution of the next line of code and leaving enough time for the AJAX call to complete and assign a value to the sel2val variable.

修复代码的正确方法是提供对该函数的回调,您将在其中执行必要的操作:

The proper way to fix your code is to provide a callback to this function where you will perform the necessary actions:

dochange('selection2', sel1val, function(result) {
    $("#select2").val(result);
});

在您的 dochange 函数中,您将在AJAX调用成功时调用此回调.

And in your dochange function you will invoke this callback in the success event of your AJAX call.

这篇关于除非有警报,否则功能无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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