开发者

Getting "Expected ',' or '{' but found '[selector]'" error

开发者 https://www.devze.com 2022-12-27 02:21 出处:网络
Getting \"Expected \',\' or \'{\' but found \'#44559\'\" error.My code looks like this: var valueid = $(\"div#center-box div#empid-textbox input\").val(); //valueid=44559

Getting "Expected ',' or '{' but found '#44559'" error. My code looks like this:

var valueid = $("div#center-box div#empid-textbox input").val(); //valueid=44559
if($("div#esd-names li#" + valueid).length > 0){
   //DO SOMETHING;
};

I'm getting the value of what is entered into a开发者_高级运维 textbox input field which in case is "44559" can't seem to figure out why I'm getting this error.

I call my valueid retrieving function with the following code. After you press ENTER in a specific textbox the value of the textbox is retrieved and ran against list items to see if it exists...if it does -- //DO SOMETHING//

$("div#center-box div#empid-textbox input.id").keypress(function(e){
  key = e.which;
  if(key===13){
    valueid = $("div#center-box div#empid-textbox input").val();
    if($("div#esd-names li[class*='" + valueid + "']").length > 0){
       //DO SOMETHING;
    };
  };
});


You are using a number as an id. This is not allowed.


Put the number in a rel attribute, and check for that $("div#esd-names li[rel=" + valueid + "]")


What are you trying to check? if the list item exists?

*/********* EDITED **********/*

I tried to recreate it so this is what I have:

HTML:

<div id="center-box">
            <div id="empid-textbox">
                <input type="text" class="id" />
            </div> 
        </div>
        <div id="esd-names">
            <ul>
                <li class="1">John Doe</li>
                <li class="2">Jane Doe</li>
                <li class="4">John Smith</li>
                <li class="8">Jane Smith</li>
            </ul>
        </div>

javascript:

$(document).ready(function()
{
                $("div#center-box div#empid-textbox input.id").keypress(function(e)
                {
                    key = e.which;
                    if (key === 13)
                    {
                        valueid = $("div#center-box div#empid-textbox input").val();

                        /*this is the only thing I changed*/
                        if ($("div#esd-names li[class*='" + valueid + "']") != null)
                        {
                            //DO SOMETHING;
                            $("div#esd-names li[class*='" + valueid + "']").css("background-color", "red");
                        }
                    }
                });
}

And it highlights the correct li every time .. hopefully you can use this .. sorry if I wasn't of any help.

0

精彩评论

暂无评论...
验证码 换一张
取 消