简体   繁体   English

无法在 AJAX 调用之前使用 JS 修改元素

[英]Unable to modify element with JS before AJAX call

The problem:问题:

I was unable to find any resources online regarding changing elements before and after an ajax request.我无法在网上找到任何关于在 ajax 请求之前和之后更改元素的资源。 If I have a <p> element with id="myText" , if I want to change the innerHTML of that element before the AJAX request, and after, this is not possible.如果我有一个id="myText"<p>元素,如果我想在AJAX请求之前和之后更改该元素的innerHTML ,这是不可能的。

I essentially want to change the text of #myText to "loading..." on click of the button, then run my AJAX request, and within that request, on success it changes that elements text from "loading...", to data , which works.我基本上想在单击按钮时将#myText的文本更改为“正在加载...”,然后运行我的AJAX请求,并在该请求中成功将元素文本从“正在加载...”更改为data ,有效。 However, the "loading..." does not show.但是,“正在加载...”没有显示。

When I check devTools, I can see that the innerHTML is indeed changing to "Loading...", but it just does not show.当我检查 devTools 时,我可以看到 innerHTML 确实正在更改为“正在加载...”,但它只是没有显示。 If I remove the AJAX request, the element successfully changes to "loading..."如果我删除AJAX请求,则元素成功更改为“正在加载...”

$(function() {
    $('#uploadBtn').click(function() {
        document.getElementById('myText').innerHTML = 'loading...'
        var form_data = new FormData($('#myForm')[0]);
        $.ajax({
            type: 'POST',
            url: '/flaskFunction',
            data: form_data,
            contentType: false,
            cache: false,
            processData: false,
            async: false,
            success: function(data) {
                document.getElementById('myText').innerHTML = data
            }
        });
    });
});

How about this, just set the value on click and if there is an error show it or just clear the element.怎么样,只需在单击时设置值,如果有错误显示或清除元素。

$(function() {
    $('#upload-file-btn2').click(function() {
        var form_data = new FormData($('#myForm')[0]);
        document.getElementById('textArea').innerHTML = 'loading...';
        $.ajax({
            type: 'POST',
            url: '/flaskFunction',
            data: form_data,
            contentType: false,
            cache: false,
            processData: false,
            async: false,
            beforeSend: function() {
            },
            success: function(data) {
                document.getElementById('textArea').innerHTML = data
            },
            error: function(data) {
            
                document.getElementById('textArea').innerHTML = "failed"
           }
        });
    });
});

If an element with id "textArea" is an actual textarea tag then you can't set the content of it with innerHTML.如果一个 id 为“textArea”的元素是一个实际的textarea标签,那么你不能用 innerHTML 设置它的内容。 You need to use value, like any form control.你需要使用值,就像任何表单控件一样。

document.getElementById('textArea').value = 'loading...'

I actually can't get your example to NOT work 😄 Is it possible that the text is actually changing, but the request executes so quickly that you just don't see it on the screen?我实际上无法让您的示例不起作用😄 是否有可能文本实际上正在更改,但请求执行得如此之快以至于您在屏幕上看不到它? If you can see the change in devtools, it should be happening.如果您可以看到 devtools 中的变化,那么它应该正在发生。 Try this in your success() function to wait 2 seconds before changing the text from 'loading...' to data :在您的success()函数中尝试此操作,等待 2 秒,然后将文本从'loading...'更改为data

setTimeout(function () {
    document.getElementById('myText').innerHTML = data
}, 2000)

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

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