简体   繁体   中英

conflicting focus on two fields inside the ajax success function used for validation

I have used ajax for validation on field blur (focus if invalid), but it conflicts with the ajax validation on next field

1.Tried setting a global variable and checking it in the next field validation. 2. Stored $(this) in variable and also called event on bind method. 3. Tried disabling other fields when focus is on current field but does not work on certain conditions.

$('input[name="ledgercode"]')
.bind(
'blur',
   '#ledgercode',
function(e) {
if ($('#dialog1').dialog('isOpen')) {
    e.preventDefault();
} else {
    $this = $(this);
var ledgercd = $(this).val();
if (ledgercd == "") {                            
   document.getElementById("msgbox1").innerHTML = "Code must be entered";

  $(this).focus();
  } else {
  $.ajax({
type : 'POST',
url : 'GL_Servlet?method=ledger_validation',
    dataType : 'json',
    data : {
    "json" : [ ledgercd,company ]
},
   success : function(data) {
    var ldgstat = (data.ldgstat);
    if (ldgstat == false) {
$this.focus(); //consistent focus if value invalid
    document.getElementById("msgbox1").innerHTML = "Invalid Code";
} else if (data.ldgstat == true) {
$('#txt_ldg_name').val(data.ldg_desc);
$this.closest("tr")
.find('input[name="ldg_desc"]').val(data.ldg_desc);
    document.getElementById("msgbox1").innerHTML = "";
}
    }                                            
   });
}
}
   });

If value on field is invalid the field should remain focused and not leave field till right value is entered.

通过在ajax问题中设置(async:false)来解决问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM