简体   繁体   中英

jquery variable using input field Name

I have a code like this:

var formFocus = formFocus || {
    spanElement: ".focusOnLoad form input[name="FirstName"]";
    init: function () {
       $(document).ready(function() {
          //come other computation
          $(formFocus.spanElement).focus();  
        });
    },
};

I am running into issues with this line: spanElement: ".focusOnLoad form input[name="FirstName"]";

I get an error at name="FirstName"

I tried escaping " with spanElement: ".focusOnLoad form input[name=\\"FirstName\\"]"; Then I get Unexpected token ; error.

I can get the code working if I just use .focusOnLoad form input[name="FirstName"] as selector. But I need to use a variable since ist a shared code and some others without the knowledge of this part has to use this variable.

Is there a way to fix this?

var formFocus = formFocus || {
spanElement: ".focusOnLoad   form input[name='FirstName']",
 init: function () { 
 $(document).ready(function() { 
 //come other computation 
 $(formFocus.spanElement).focus();
   }); }, };

You have here, you wrote

form input[name="FirstName"]";

Instead of:

form input[name='FirstName']",

The difference is: you terminated the line with semicolon instead of a comma, and also, you used double quote inside another double quote

Hope my answer helps 😉

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