37363

jQuery blur keeps firing when two inputs are involved

<h3>Question</h3>

I've dug around for a while on Google and here on stack Overflow and can't find anything related to this.

I've got multiple input textfields that when blurred calls a jQuery AJAX function to the server. It works beautifully, almost.

The problem is if I have focus on one text input and then click on another one (making it in focus). The blur event keeps getting fired with AJAX calls to the server and alerts.

Any ideas how to calm blur down so it isn't constantly firing? There is probably something very simple that I'm not doing that might fix this.

Here's the code (ignore the {{}} and {%%} tags, this is a django template).

The inputs:

<td class="tracking-text">CK Req:</td><td class="tracking-td"><input class="tracking-input" type="text" value="{{pr.checkreq|default_if_none:"&nbsp"}}" size="10"></td> <td class="tracking-text">LPD:</td><td class="tracking-td"><input class="tracking-input" type="text" value="{{pr.lpd|default_if_none:""}}" size="10"></td> <td class="tracking-text">PR#</td><td class="tracking-td"><input class="tracking-input" type="text" value="{{pr.prnumber|default_if_none:""}}" size="10"></td> <td class="tracking-text">DATE:</td><td class="tracking-td"><input class="tracking-input" type="text" value="{{pr.prdate|default_if_none:""}}" size="10"></td>

The jQuery:

$('.tracking-input').blur(function(){ $.post( "{% url buy.views.update_purchase_ajax %}", {pr_pk: $('span#pr_pk').html()}, function(data){ alert(data.message); }, 'json'); });

Any ideas would be greatly appreciated.


<h3>Answer1:</h3>

The alert is causing you to again lose focus, firing off another AJAX request.

Maybe fix it by only sending the AJAX request if the input is not empty?

Is this alert really needed or is it just for debugging? This is a prime example why you shouldn't use alert for debugging. Use console.log.


<h3>Answer2:</h3>

Your problem is the alert() itself.

It takes the focus away from the input when it is shown so it keeps recurring.

来源:https://stackoverflow.com/questions/6550331/jquery-blur-keeps-firing-when-two-inputs-are-involved

Recommend

  • Spatial Index not being used
  • How do I serialize a map of type [string]reflect.Value?
  • How to vectorize comparing each row of matrix with all other rows
  • Cosmosdb Saved data cannot be found in portal but from azure-documentdb-spring-boot-starter
  • How can I set logging level for Heroku Postgresql?
  • Merkle tree for difference comparison, in Cassandra
  • Github API: Fetch issues with exceeds rate limit prematurely
  • UnsatisfiedLinkError : setMessageHandler(Native Method)
  • Can I recreate a temp table after dropping it?
  • Google Chrome won't allow IFRAME to load an HTML file
  • C - Freeing memory after strdup()
  • How to sort an associative array in PL/SQL?
  • What is the format of in git-branch --list
  • Client-side HTTP_ACCEPT_LANGUAGE access? JavaScript? I don't see a JQuery wrapper. Static pages
  • Issue while running Linux Command using Java?
  • Perl is respecting '
  • Bind a function to multiple dynamically created buttons in kivy?
  • How to pivot table for year in SQL?
  • How well does Entity Framework 6 support .NET 4.0?
  • TypeError: object is not a function showing at express
  • Linking to an id not working in Firefox - Working in Chrome/Safari
  • REGEXP_REPLACE pattern has to be const? Comparing strings in BigQuery
  • Dynamic reference casting depending on actual object type
  • in Gwt, there are 2 different packages (or 2 options) for doing drag n Drop? Which one is better?
  • Bulk loading into PostgreSQL from a remote client
  • why 'read' command in shell script is missing initial characters? [duplicate]
  • Laravel 5 - Cache remember doesn't work
  • Opening tel: links from UIWebView
  • Extracting a small subset of data from XMLs
  • Find all parks for a given zipcode with google maps
  • How to control xtics in gnuplot
  • Run a form (insert/update/delete) from within a div using jquery
  • How to resolve this in PHPUnit where it is asking me to set KERNEL_DIR in my phpunit.xml?
  • Terminal run dalvikvm with am.jar
  • How to handle div that is created dynamically in a table
  • Google App Engine backend servlet not responding
  • ssh remote server login script
  • Make checkout phone field optional for specific countries in WooCommerce
  • ReferenceError: TextEncoder is not defined