简体   繁体   English

Ajax 调用后的 Dom 值

[英]Dom Value After Ajax Call

I have a table where one field is titled a secure password.我有一张表,其中一个字段名为安全密码。 In this field, there is an input button that calls an AJAX function.在该字段中,有一个调用 AJAX 函数的输入按钮。 The AJAX function on return updates the password in the secured password field.返回的 AJAX 函数更新安全密码字段中的密码。 This updates the DOM value for that field.这将更新该字段的 DOM 值。

On this same page I have another function which uses Javascript to parse all the elements in the table into an array.在同一页面上,我有另一个函数,它使用 Javascript 将表中的所有元素解析为一个数组。 The problem is the Javascript function is writing what was originally in the secure password field rather than what the AJAX function has updated it to.问题是 Javascript 函数正在写入最初在安全密码字段中的内容,而不是 AJAX 函数已将其更新为的内容。

It seems as though Javascript does not pull the current DOM value but the DOM value when the page was loaded.似乎 Javascript 不会提取当前的 DOM 值,而是在加载页面时提取 DOM 值。 Is this the default nature of Javascript and if so how can I get around this so I can get the current values for all fields in the table, not just the page load values.这是 Javascript 的默认特性吗?如果是,我该如何解决这个问题,以便我可以获取表中所有字段的当前值,而不仅仅是页面加载值。

This script is written in PHP/Javascript, thanks in advance for the assistance.该脚本是用 PHP/Javascript 编写的,在此先感谢您的帮助。

Your Javascript function that parse all elements in the table into an array uses the document.ready function or is loaded only once when the Page is loaded.将表格中的所有元素解析为数组的 Javascript 函数使用document.ready函数或仅在页面加载时加载一次。

In Vanilla JS在香草 JS 中

document.addEventListener('DOMContentLoaded', (event) => {
   //Where you wrote your function
})

Jquery查询

$(document).ready(function() {
    // where you have your function if you are using Jquery
});

And AJAX calls do not reload the page.并且 AJAX 调用不会重新加载页面。 That means your Javascript function does not get executed so it does not update the array with the new data from the calls.这意味着您的 Javascript 函数不会被执行,因此它不会使用来自调用的新数据更新数组。

You need your Javascript as a callback function to the success or complete property of the AJAX call.您需要您的 Javascript 作为 AJAX 调用的successcomplete属性的回调函数。

jQuery.ajax({
    type: 'POST',
    url: ajaxurl,
    success: function() {

        parsingFunction();

    },
    error: function() {
        alert(errorThrown);
    }
});

Did you try using a hidden input field for storing the data instead of updating the text input?您是否尝试使用隐藏输入字段来存储数据而不是更新文本输入? Or may be you can try using the data-xxx attributes或者您可以尝试使用data-xxx 属性

bests,最好的,

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM