How to display loading spinner in a textbox on clicking of a button?

时间秒杀一切 提交于 2019-12-05 18:15:57

问题


How to display loading spinner in a textbox on clicking of a button? Say I have a username and password textbox, and a login button. Once i enter username and password and clicking on Login button, the spinners should be displayed in the textboxes. Kindly help. Iam looking for a JQuery option or Javascript.


回答1:


Let's say your html has following textbox :

<input id='inputsource' />

Define a new css class

.loadinggif 
{
   background:
     url('http://www.hsi.com.hk/HSI-Net/pages/images/en/share/ajax-loader.gif')
     no-repeat
     right center;
}

and then adding this class to your testbox by jquery code:

$('#inputsource').addClass('loadinggif');

Jsfiddle: http://jsfiddle.net/msjaiswal/FAVN5/




回答2:


Just add the "spinner" image as a background image to your inputs when submitting the form :

CSS:

input.spinner {
    background-image:url('spinner.gif');
    background-repeat:no-repeat;
    background-position:5px 5px /* Change to accommodate to your spinner */
}

JS:

$('form').submit(function(e){
    e.preventDefault();
    $f = $(this);
    $f.submit(); /* replace with your ajax call */
    $f.find('input[type="text"], input[type="password"]')
        .val('') /* Remove values or store them if you need them back */
        .addClass("spinner") /* Add the spinning image */
        .attr("disabled", "disabled"); /* Disable the inputs */
});


来源:https://stackoverflow.com/questions/12837335/how-to-display-loading-spinner-in-a-textbox-on-clicking-of-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!