简体   繁体   English

尝试更改提交按钮的跨度类以显示引导加载微调器

[英]Trying to change the span class for a submit button to show bootstrap loading spinner

I'm trying to get a submit button to change to a loading spinner with the text "Loading..." using JavaScript like the image below.我正在尝试使用 JavaScript 将提交按钮更改为带有文本“正在加载...”的加载微调器,如下图所示。

在此处输入图片说明

I'm able to update the innerHTML for the button, but I'm having trouble changing the span class, which controls the actual spinner.我能够更新按钮的innerHTML,但是我无法更改控制实际微调器的span 类。

Here is my html for the button:这是我的按钮 html:

<button class="btn btn-primary mx-3" id="submit" onclick="loading()" type="submit">
  <span id="button_span"></span>
  Submit
</button>

And here is the JavaScript:这是 JavaScript:

function loading() {
    var button = document.getElementById("submit");
    button.innerHTML = "Loading...";
    var span = document.getElementById("button_span");
    span.classList.add("spinner-grow");
    span.classList.add("spinner-grow-sm");
}

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks.谢谢。

Here you have a working pen: https://codepen.io/cbrown___/pen/dyMKQQb在这里你有一支工作笔: https : //codepen.io/cbrown___/pen/dyMKQQb

Using Jquery and Font-awesome.使用 Jquery 和 Font-awesome。 THere are many ways of doing this, but this is one of them.有很多方法可以做到这一点,但这是其中之一。

HTML HTML

<button class="btn btn-primary mx-3" id="submit" onclick="loading()" type="submit">
   <i class="fas fa-spinner fa-spin" style="display:none;"></i>
  <span class="btn-text">Submit</span>
</button>

.JS .JS

function loading() {
  $(".btn .fa-spinner").show();
  $(".btn .btn-text").html("Loading");
  
}

Recommend also this solution: Bootstrap 4 Loading Spinner in button还推荐这个解决方案: Bootstrap 4 Loading Spinner in button

I don't think you should be changing the inner HTML or class of the button.我认为您不应该更改按钮的内部 HTML 或类。 Create a new span that is the loading animation but hidden by default with css prop display: none .创建一个新的跨度,它是加载动画,但默认情况下使用 css prop display: none隐藏。

When the button get clicked hide the button by dynamically changing the css and show the span the same way.当按钮被点击时,通过动态更改 css 隐藏按钮并以相同的方式显示跨度。 Then whenever the loading is done just flip it back.然后无论何时加载完成,只需将其翻转回来。

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

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