簡體   English   中英

在onblur驗證字段后設置焦點

[英]setting focus following validation of field with onblur not working

在花了一個小時或更長時間尋找其他選擇之后,我空了出來。 一旦失去焦點,就會調用一個函數,在驗證失敗時將焦點重新設置到調用字段的部分不起作用...

此處的示例: http : //owenparker.com/test/testval.php

 var emptyString = /^$/; function validatePresent(valfield, infofield) { var stat = commonCheck(valfield, infofield, true); if (stat != proceed) return stat; msg(infofield, "warn", ""); return true; } var proceed = 2; function commonCheck(valfield, infofield, required) { if (!document.getElementById) return true; // not available on this browser - leave validation to the server var infoelem = document.getElementById(infofield); if (!infoelem.firstChild) return true; if (infoelem.firstChild.nodeType != Node.TEXT_NODE) return true; if (emptyString.test(valfield.value)) { if (required) { msg(infofield, "error", "ERROR: required"); setfocus(valfield); return false; } else { msg(infofield, "warn", ""); // OK return true; } } return proceed; } function msg(fld, msgtype, message) { var dispmessage; if (emptyString.test(message)) dispmessage = String.fromCharCode(160); else dispmessage = message; var elem = document.getElementById(fld); elem.firstChild.nodeValue = dispmessage; elem.className = msgtype; } function setFocusDelayed() { global_valfield.focus(); } function setfocus(valfield) { // save valfield in global variable so value retained when routine exits global_valfield = valfield; setTimeout( 'setFocusDelayed()', 100 ); } 
 <table border=0 width=500px> <tr> <td>Misc 1:&nbsp; <input name="misc1" id="misc1" type="text" size="10" tabindex=1 autofocus onblur="validatePresent(this, 'inf_misc1');"> <div id="inf_misc1">&nbsp;</div> </td> </tr> <tr> <td>Whatever:&nbsp; <select name="whatever" id="whatever" tabindex=2 onblur="validatePresent(this, 'inf_whatever');"> <option value="" SELECTED>Choose</option> <option value="OPT1">Option 1</option> <option value="OPT2">Option 2</option> </select> <div id="inf_whatever">&nbsp;</div> </td> </tr> </table> 

嘗試這個:

function setfocus(valfield)
{

  setTimeout( function () {
      valfield.focus();
  }, 100 );
}

使用全局變量將閉包傳遞給setTimeout w / o。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM