简体   繁体   English

使用jQuery定位dom元素不起作用

[英]Targeting dom element with jQuery not working

I am using a JS Promise to asynchronously get the user's location inside getLocation() . 我正在使用JS Promise异步获取getLocation()用户的位置。 And then I'm making an Ajax request to the server inside postLocation() . 然后,我向postLocation()的服务器发出Ajax请求。

$('#add_location_btn').on('click', function () {
        if ($('#code').val().length === 0) {
            window.alert('Enter a valid code!');
        } else {
            getLocation().then(function (pos) {
                $('#addlocation_loader').attr('hidden', false);  // Show loading sign
                return pos;
            }).then(function (pos) {
                postLocation(pos);
            });
        }
        $('#addlocation_loader').attr('hidden', true);  // Hide loading sign
    });

However, eventually changing addlocation_loader 'hidden' attribute to true is not working, meaning that the attribute is properly set to false but never turns true. 但是,最终将addlocation_loader '属性更改为true无效,这意味着该属性已正确设置为false,但从不变为true。

Edit 编辑

It's worth noting that I want to hide the loading sign after postLocation() is executed. 值得注意的是,我想在执行postLocation()之后隐藏加载符号。

I have tried setting 'hidden' to true in a third then() statement, but the sign now never shows up. 我尝试在第三then()语句中将“ hidden”设置为true,但是现在从不显示该符号。 It seems that the show and hide statements are quickly executed after one another, which is confusing (When I comment out the hide statement the sign is normally shown, which means that both execute). 似乎show和hide语句很快就彼此执行了,这很令人困惑(当我注释掉hide语句时,通常会显示符号,这意味着它们都将执行)。

getLocation().then(function (pos) {
    $('#addlocation_loader').attr('hidden', false);  // Show loading sign
    return pos;
}).then(function (pos) {
    postLocation(pos);
}).then(function () {
    $('#addlocation_loader').attr('hidden', true);  // Hide loading sign
});

If you want to hide the loader, you should do it within the .then() callback, because that is when the promise has been resolved. 如果要隐藏加载程序,则应在.then()回调中完成该操作,因为这是在Promise已解决时。 So what you want to do is: 因此,您要做的是:

  1. Show loader before executing postLocation() 在执行postLocation()之前显示加载程序
  2. Remember to return the promise from postLocation() (which you didn't do in the code) 记住要从postLocation()返回承诺(您在代码中没有这样做)
  3. Hide the loader when the promise is resolved (after posting the position has succeeded) 兑现诺言后,隐藏装载程序(过帐位置成功后)

Here is your fixed code: 这是您的固定代码:

// Perform async operation
getLocation().then(function (pos) {

    // Show loader
    $('#addlocation_loader').attr('hidden', false);

    // Post location. Remember to return the promise to chain it!
    return postLocation(pos);

}).then(function() {

    // Hide loader when position is successfully posted
    $('#addlocation_loader').attr('hidden', true);  

});

You are using an asynchronous function to the attribute to false. 您正在使用一个异步函数将该属性设置为false。 That means that probably 这意味着

$('#addlocation_loader').attr('hidden', true);

is executed before 在执行之前

$('#addlocation_loader').attr('hidden', false);

You may have your hide/show loader backwards. 您可能会将您的隐藏/显示加载器向后移动。 Looks Like you set hidden to false when the location is returned and to true when the button is pressed. 看起来像您在返回位置时将hidden设置为false,而在按下按钮时将其设置为true。

Perhaps something like this would work: 也许这样的事情会起作用:

$("#add_location_btn").on("click", function() {
  const $loader = $("#addlocation_loader");
  if ($("#code").val().length === 0) {
    window.alert("Enter a valid code!");
  } else {
    $loader.attr("hidden", false); // Show when requested
    getLocation()
      .then(function(pos) {
        $loader.attr("hidden", true); // Hide when returned
        postLocation(pos);
      });
  }
});

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

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