javascript - jquery multiple selector with contains() -
i've lot of <option>
s in <select>
and , attribute called zip
<select id="city"> <option zip="auc 1291 100005" value="1">city 1</option> <option zip=" 1295 100006" value="2">city 2</option> <option zip=" 1299 100008" value="3">city 3</option> </select>
now want select option text or zip contains string value, following code doens't work.
var val = '1291'; //zip code var x = $('#city option:contains('+val+'), #city zip:contains('+val+')').val();
why?
if want select elements attribute, taht value contains requested string, have use:
var val = '1291'; //zip code var x = $('#city option[zip*="' + val + '"]').val();
as mentioned: http://api.jquery.com/attribute-contains-selector/
but guys said, better way have html5 data-zip
attribute instead of zip
(just prefix data-
).
in case, selector be:
var x = $('#city option[data-zip*="' + val + '"]').val();
hope helped.
Comments
Post a Comment