简体   繁体   中英

JQuery: setting focus on an input field on a specific page

I am trying to set focus on Customer Name only on the search_orders.php page. The code below is supposed to put the cursor in the Name input if on the order search page. Somehow this doesn't work. Please suggest any change to my code that will fix this. Attached is the UI for searching orders, where I need the focus set on Customer Name.

在此处输入图片说明

function po_number_first(){
   if (document.location.href.indexOf("search_orders")==-1){
      document.search_form.po_number.focus();
}else{
     document.getElementById("Name").focus();
}
}

The function call is in a smartyTemplate files as seen in the code below.

{include file="header.tpl" page_name=Search extra_javascript='<script language="JavaScript" src="includes/search_orders.js"></script>' on_load_script='po_number_first()'}

Instead of adding it inside a function please try:-

jQuery(document).ready(function() {
   if ( window.location.href.indexOf('/search_orders') > -1 ) {
     jQuery( "#target" ).focus();
   }else{
     jQuery( "#Name" ).focus();
   }
});

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