开发者

Ajax spinner on multiple inputs in same form

开发者 https://www.devze.com 2023-04-04 04:06 出处:网络
I have a spinner that displays when an ajax request is made using jquery: $(document).bind(\"ajaxStart\", function() {

I have a spinner that displays when an ajax request is made using jquery:

$(document).bind("ajaxStart", function() {

  $('#myloader').show();

});

$(document).bind("ajaxStop", function() {

  $('#myloader').delay(1000).fadeOut('fast');

});

The request is actually made on keyup in this case, I'm trying to work out how to use this spinner on multiple fields, so the spinner would display next to each field.

Placing the spinner image isn't the problem, but connecting the the image to each field is what i'm finding difficult. If that makes sense?

--UPDATE--

The answers are very helpfull but I dont think its what im looking for.

I have the following code:

<div class="form_element">
  <%= f.label :email, :class=>"field_hint", :title=>"Email" %>
  <%= f.text_field :email %>
  <div class="myloader" style="display:none;">
    <p>
    validating...
    </p>
  </div>
</div>

<div class="form_element">
  <%= f.label :username, :class=>"field_hint", :title=>"Username" %>
  <%= f.text_field :username %>
    <div class="myloader" style="display:none;">
      <p>
      validating...
      </p>
    </div>
</div>

Both of these fields have a keyup that does a remote call. I have changed the code slightly:

$('.form_element').ajaxStart(function() {
  $(this).开发者_Go百科children('.myloader').show();
});

$('.form_element').ajaxStop(function() {
  $(this).children('.myloader').delay(1000).fadeOut('fast');
});

At the moment when an the ajax happens both spinners appear, I am trying to work out how to make each spinner div relevant to its field.

I am new to Ajax so any advice on maybe doing the ajaxStart/Stop differently are more than welcome.


Were you looking for something like this?

 $("input").ajaxStart(function(){
      // Access the field
      if($(this).attr('id') == idThatTriggeredRequest) {
           $("#myloader").show();
           // Position spinner
      }
 });

EDIT

Here is another solution that might work for you assuming the event is always keyUp (you could also abstract this into a plugin, passing the event, in this case 'keyup' as an option):

 $("input").bind('keyup', function(){

    // Show the spinner
    $("#myloader").show();

    // Initiate the ajax request
    $.ajax({
         // Handle complete, hide spinner
    });
 });


You could do this multiple ways. I don't know how your structure looks, but consider this purely untested and probably wildly broken code snippet:

var spinner = '<img class="spinner" />';

$('#myelement').click(function() {
  var $el = $(this),
      $sp = $(spinner).hide().appendTo($el).fadeIn();

  $el.ajax({
    // ... options to load me!
    complete: function() {
      $sp.delay(1000).fadeOut('fast', function() { $sp.remove() });
    }
  });
});

Rough, but you get the idea. Depending on how you're handling your Ajax, there may be much more elegant ways to handle this using global callbacks, rather than having to manage the spinner for each element.

0

精彩评论

暂无评论...
验证码 换一张
取 消