javascript - Syntax error, unrecognized expression: option[value=property name] - Stack Overflow

I have a datalist which looks like this<datalist id="properties"><option value="

I have a datalist which looks like this

<datalist id="properties">
       <option value="property name"></option>
       <option value="property"></option>
</datalist>

Now I'm using this code to find where values entered by the user is in the list:

var user_property = $('#user_property').val().toLowerCase(); // taken from input type with id user_property
var pro = $('#properties').find("option[value="+user_property.replace(' ','-')+"]");
if(pro != null && pro.length > 0)
{
    // run some code
}
else
{
    // show error popup
}

I am getting error in var pro = $('#properties').find("option[value="+user_property.replace(' ','-')+"]");

Error code says Syntax error, unrecognized expression: option[value=property name]

How to get rid of this error?

I have a datalist which looks like this

<datalist id="properties">
       <option value="property name"></option>
       <option value="property"></option>
</datalist>

Now I'm using this code to find where values entered by the user is in the list:

var user_property = $('#user_property').val().toLowerCase(); // taken from input type with id user_property
var pro = $('#properties').find("option[value="+user_property.replace(' ','-')+"]");
if(pro != null && pro.length > 0)
{
    // run some code
}
else
{
    // show error popup
}

I am getting error in var pro = $('#properties').find("option[value="+user_property.replace(' ','-')+"]");

Error code says Syntax error, unrecognized expression: option[value=property name]

How to get rid of this error?

Share Improve this question edited Jan 28, 2015 at 17:53 k0pernikus 66.4k77 gold badges240 silver badges359 bronze badges asked Jan 28, 2015 at 17:28 runningmarkrunningmark 7604 gold badges13 silver badges33 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 20

try adding quotes, as:

var pro = $('#properties').find("option[value='"+user_property.replace(' ','-')+"']");

or better break it down to:

var replaced = user_property.replace(' ','-');
var pro = $('#properties').find("option[value='"+replaced+"']");

if you want to check for text like "property name" then you could directly do:

var pro = $('#properties').find("option[value='"+user_property+"']");

Try adding quotes around the value and it will work.

$('#properties').find("option[value='property name']")

You need to add single quote for your value like

var pro = $('#properties').find("option[value='"+user_property.replace(' ','-')+"']");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1738457076a4050315.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信