javascript:创建适当的函数来加载按钮?

huangapple go评论68阅读模式
英文:

javascript : Create a suitable function for loading the button?

问题

我有一个表单,我希望在表单按钮提交时在按钮上显示加载状态。我创建了这个模式,但它很简单。我想创建一个可以在所有表单中使用的函数,请帮助我,我不太懂jQuery?

const btnsabt = $('.btn-sabt');
btnsabt.addClass('submit-btn');
btnsabt.prop('disabled', true);
btnsabt.find('i').prop('hidden', false);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<form id="MobileUserForm" method="post">
  <div class="form-group">
    <input asp-for="Mobile" type="number" class="form-control" />
  </div>
  <div class="form-group">
    <button type="submit" class="btn btn-primary btn-sabt">
       <i class="fa fa-refresh fa-spin" hidden></i>保存
    </button>
  </div>
</form>
英文:

I have a form, I want a loading to be displayed on the button when the form button is submitted.
I created this mode but in a simple mode.
I want to create a function that I can use in all forms, please help me, I am not fluent in jQuery?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const btnsabt = $(&#39;.btn-sabt&#39;);
btnsabt.addClass(&#39;submit-btn&#39;);
btnsabt.prop(&#39;disabled&#39;, true);
btnsabt.find(&#39;i&#39;).prop(&#39;hidden&#39;, false);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&quot;&gt;

&lt;form id=&quot;MobileUserForm&quot; method=&quot;post&quot;&gt;
  &lt;div class=&quot;form-group&quot;&gt;
    &lt;input asp-for=&quot;Mobile&quot; type=&quot;number&quot; class=&quot;form-control&quot; /&gt;
  &lt;/div&gt;
  &lt;div class=&quot;form-group&quot;&gt;
    &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary btn-sabt&quot;&gt;
       &lt;i class=&quot;fa fa-refresh fa-spin&quot; hidden&gt;&lt;/i&gt;save
    &lt;/button&gt;
  &lt;/div&gt;
&lt;/form&gt;

<!-- end snippet -->

答案1

得分: 1

Sure, here are the translated parts:

不确定 btnsabt.addClass('submit-btn'); 是用来干什么的,如果需要,请取消注释

调用 toggleLoader(1); 来启动旋转,调用 toggleLoader(0); 来停止

任务理想情况下应该是异步的,因为界面需要更新

const toggleLoader = show => {
  $('.btn-sabt')
    //  .toggleClass('submit-btn', show)
    .prop('disabled', show)
    .find('i').toggleClass('hide', !show)
};

$(function() {
  $('#MobileUserForm').on('submit', (event) => {
    event.preventDefault(); // 防止表单提交

    toggleLoader(1);  // 显示旋转器并禁用按钮

    // 执行任务
    // 完成后移除加载动画

    // 用于显示延迟以可视化加载动画
    setTimeout(() => { // 移除此行
      toggleLoader(0); // 隐藏旋转器 - 在任务结束时调用
    }, 3000); // 移除此行
  });
});
.hide{
  display: none !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<form id="MobileUserForm" method="post">
  <div class="form-group">
    <input asp-for="Mobile" type="number" class="form-control" />
  </div>
  <div class="form-group">
    <button type="submit" class="btn btn-primary btn-sabt">
       <i class="fa fa-refresh fa-spin hide"></i> 保存
    </button>
  </div>
</form>
英文:

Not sure what the btnsabt.addClass(&#39;submit-btn&#39;); is for, if needed uncomment it

Call toggleLoader(1); to spin and toggleLoader(0); to stop

the task should ideally be async since the interface needs to update

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const toggleLoader = show =&gt; {
  $(&#39;.btn-sabt&#39;)
//  .toggleClass(&#39;submit-btn&#39;,show)
  .prop(&#39;disabled&#39;, show)
  .find(&#39;i&#39;).toggleClass(&#39;hide&#39;,!show)
};

$(function() {
  $(&#39;#MobileUserForm&#39;).on(&#39;submit&#39;, (event) =&gt; {
    event.preventDefault(); //prevent form submission
    
    toggleLoader(1);  // show the spinner and disable

    //perform tasks
    //after completion, remove the loading
    
    //test to show a delay to visualise the loader
    setTimeout(() =&gt; { // remove this line
      toggleLoader(0); // hide the spinner - call this at the end of the task
    }, 3000); //remove this line

   
  });
});

<!-- language: lang-css -->

.hide{
  display: none !important;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&quot;&gt;

&lt;form id=&quot;MobileUserForm&quot; method=&quot;post&quot;&gt;
  &lt;div class=&quot;form-group&quot;&gt;
    &lt;input asp-for=&quot;Mobile&quot; type=&quot;number&quot; class=&quot;form-control&quot; /&gt;
  &lt;/div&gt;
  &lt;div class=&quot;form-group&quot;&gt;
    &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary btn-sabt&quot;&gt;
       &lt;i class=&quot;fa fa-refresh fa-spin hide&quot;&gt;&lt;/i&gt; save
    &lt;/button&gt;
  &lt;/div&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 14:26:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定