简体   繁体   English

从使用AJAX更新的列表框中获取最新值

[英]Getting the latest value from a listbox which has been updated using AJAX

I'm using two listboxes, the 2nd one is updated by AJAX whenever the 1st listbox's value is changed. 我正在使用两个列表框,每当第一个列表框的值更改时,第二个列表框就会由AJAX更新。 Now, based on the values of these two listboxes I need to create a table. 现在,基于这两个列表框的值,我需要创建一个表。 But the problem is that my table is created with the old value of the 2nd listbox and not the latest. 但是问题是我的表是用第二个列表框的旧值创建的,而不是最新的。

Code: 码:

<script>
function pop_to()
{
    var xmlhttp= new XMLHttpRequest();
    var from=document.getElementById("date_from").value;
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("date_to").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","pop_to.php?from="+from,true);
    xmlhttp.send();
}

function update_sales_content()
{
    var xmlhttp= new XMLHttpRequest();
    var from=document.getElementById("date_from").value;
    var to=document.getElementById("date_to").value;
    var xmlhttp= new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("sales_data").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","sales_report.php?from="+from+"&to="+to,true);
    xmlhttp.send();
}

</script>

<body>  
<select id="date_from" onchange='pop_to();update_sales_content();' style="position:relative;left:730px;">
    <option>From</option>
    <?php 
        $sql="select distinct order_dt from cust_order order by order_dt";
        $result=mysql_query($sql,$con) or die(mysql_error());
        if($result)
        {
            while($rec=mysql_fetch_row($result))
            {
                echo"<option value='$rec[0]'>$rec[0]</option>";
            }

        }
    ?>
    </select>
    <select id="date_to" onchange='update_sales_content()' style="position:relative;left:800px;">
    </select>
<div id="sales_data">
</div>
</body>

AJAX is asynchronous. AJAX是异步的。 When you do: 当您这样做时:

onchange='pop_to(); update_sales_content();'

you're running update_sales_content() immediately, not waiting for the AJAX response. 您正在立即运行update_sales_content() ,而无需等待AJAX​​响应。 You need to call it from the pop_to() callback function: 您需要从pop_to()回调函数调用它:

function pop_to()
{
    var xmlhttp= new XMLHttpRequest();
    var from=document.getElementById("date_from").value;
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("date_to").innerHTML=xmlhttp.responseText;
            update_sales_content();
        }
    }
    xmlhttp.open("GET","pop_to.php?from="+from,true);
    xmlhttp.send();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改使用ajax调用附加的html输入的span值? - How to change span value from a html input which has been appended using ajax call? 如果在提交之前未更新值,则从表单中删除隐藏的输入 - Remove hidden input from form if the value has not been updated before submitting 获取已更改的复选框值的值 - Getting the value of the checkbox value that has been changed 无法从textBox获取最新值 - Not getting latest value from textBox 从JSON获取价值,JSON使用JavaScript在密钥名称中包含空格 - Getting value from JSON, which has space in key name using JavaScript LocalStorage 未使用最新值更新 - LocalStorage not updated with the latest value 尝试获取已通过下拉选择项更新的字段的文本值 - Trying to get the text value of a field that has been updated by a dropdown selection JavaScript 如何从一个 function 中获取目标值并乘以已选中复选框的元素值 - JavaScript how to take target value from one function and multiply by element value for which the checkbox has been checked 使用JavaScript获取垂直列表框选定的值 - getting perticular listbox selected value using javascript JavaScript 在 setInterval 调用的函数中使用 screen.width 或 screen.availWidth 时未获取更新值 - JavaScript Not getting an updated value when using screen.width or screen.availWidth in a function which is called by setInterval
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM