mercredi 15 avril 2015

JQuery function submits form erroneously

I've inherited code from another developer, and an anonymous JQuery function is failing ONLY in Firefox on a Windows platform. On all other browsers, this works, and it even works in Firefox on a MAC, but not in Windows.


The issue is that if an error is detected in the code below, an Alert message is constructed. If that message is detected, then the code creates an Alert prompt box that should pause the process and halt processing. This type of procedure is employed elsewhere in the code (omitted here for brevity's sake) and it works fine. But here, on Firefox for Windows, the Alert box is quickly flashed to the screen and then the form is submitted; the code should NOT advance and auto-submit the form.


Firebug reports "Uncaught exception: Out of memory" when this procedure is run with it turned on.


I've tried adding .preventDefault(); in various locations in the code, but to no avail. Any clues?



$(function() {

$('#add_new_keyword').on('click', function() {

if ($('#new_keyword').val() != '') {
var message;
var keywordText;

// Test to see if keyword already exists in queue; if so, set Alert message:
$('#checkboxes label').each(function() {
keywordText = $(this).text().trim();
if (keywordText && keywordText.toLowerCase() == $('#new_keyword').val().toLowerCase()) {
message = 'Keyword ' + keywordText + ' has already been added.';
}
});

// Stop process with Alert message, else proceed:
if (message) {
alert(message);
} else {
var newKeyword = '<label><input name="new_keyword[]" type="checkbox" tabindex="-1" value="' + $('#new_keyword').val() + '" checked /> ' + $('#new_keyword').val() + '</label>';
$('#checkboxes').append(newKeyword);
}

$('#new_keyword').val('').focus();
}
});
});

Aucun commentaire:

Enregistrer un commentaire