简体   繁体   中英

Jquery .focus not moving to required field

I have a form with some tabs. On each tab is a required field. I am using jquery for the error validation however the .focus() is not setting focus on the field. Any suggestions?

Code:

if (dateFrom.value == null || $.trim(dateFrom.value) == '' || $.trim(dateFrom.value) == "") {
                if (sucMsg != null) {
                    sucMsg.style.visibility = "hidden";
                    divsucMsg.style.visibility = "hidden";
                }
                alert('From Date is required on the What Happened tab.');
                $("txtDateFrom").focus()
                $("txtDateFrom").addClass("errorClass");
                isValid = false;
                return false;
            }

txtDateFrom should either be identified as a class name or element ID, here you are doing neither (the selector would be looking for an element type of txtDateFrom ).

So try .txtDateFrom (select by class) or #txtDateFrom (select by ID).

I highly doubt you have a <txtDateFrom /> element

$("txtDateFrom")
  ^^

You are not selecting by id, but by element type. Add the missing #

$("#txtDateFrom")
  ^^

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