Help in jquery selector
I have form elements like this:
<input type='text' id='person_1_name'>
<input type='text' id='person_2_name'>
<input type='text' id='person_3_name'>
<input type='text' id='person_4_name'>
<开发者_JAVA百科input type='text' id='person_5_name'>
..
..
..
I can select single element like this:
$("#person_1_name").focusout( function() {
alert("Hello World");
});
Is there any selector that select all elements(person_<*>_name) like this without describing them separately ?
Thanks
this is not the right way .
you should add Class to each of the elements and then select it by :
$(".MyClass")...
but if you insist :
$('div') .filter(function() { return this.id.match(/person\_[0-9]\_name/); }) .css('border','solid 1px red')
JQuery emulated CSS selectors. So, this should work well:
$('input[id^="person_"][id$="_name"]');
$("id[id^='person_']"); //starts with
$("id[id$='_name']"); //ends with
Add a class to the person_n_name elements and use a class selector instead.
You could try by picking them with a class. First set them a class and then pick them via script
like this:
<input type='text' id='person_1_name' class='person'>
<input type='text' id='person_2_name' class='person'>
<input type='text' id='person_3_name' class='person'>
<input type='text' id='person_4_name' class='person'>
<input type='text' id='person_5_name' class='person'>
<script type="text/javascript">
var person = $(".person");
person var will contain a array
The best way is to add class='person_name'
in your input
and use
$(".person_name")focusout( function() {
alert("Hello World");
});
But if you are forced to use id
then you can use the code below
$("input[id^='person_'][id$='_name']").focusout( function() {
alert("Hello World");
});
See a working demo here http://jsfiddle.net/usmanhalalit/aPD5K/
This should work (untested)
$('input[id*="person"]')
精彩评论