Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery selector how to selector two element

<select name="d1">
  <option value="dd">111</option>
  <option value="dd">111111</option>
</select>
<select name="d2">
 <option value="dd">2222</option>
  <option value="dd">222222222222</option>
</select>

i have two select how two use jquery select this two

$("select[name='d1']").change(function(){xxx});

this code only could select one element,any one could give a hand,thanks

like image 774
mlzboy Avatar asked Nov 07 '10 04:11

mlzboy


People also ask

How can use multiple selector in jQuery?

version added: 1.0jQuery( "selector1, selector2, selectorN" ) selector1: Any valid selector. selector2: Another valid selector. selectorN: As many more valid selectors as you like.

How do you select multiple selectors?

To select multiple elements of an html page using multiple elements selector, we pass the element names inside parenthesis, in double quotes, separated by commas. For example: $(“div, p, h2”) this will select all the div, p and h2 elements of a page.

Can I select multiple ID in jQuery?

Given an HTML document and the task is to select the elements with different ID's at the same time using JQuery. Approach: Select the ID's of different element and then use each() method to apply the CSS property on all selected ID's element.


3 Answers

There are several options.

  1. Select all select elements on the page:

    $("select").change(function(){xxx});
    
  2. Select only those select elements contained within a form having the ID formD:

    $("form#formD select").change(function(){xxx});
    
  3. Select only those select elements in class d (add a class HTML attribute to each select element):

    $("select.d").change(function(){xxx});
    
  4. Select only those select elements whose names/IDs begin with d:

    $("select[name^=d]").change(function(){xxx});
    
  5. Select only those select elements specifically named by ID (add an id HTML attribute to each select element):

    $("select#d1, select#d2").change(function(){xxx});
    
  6. Select only those select elements specifically named using the name attribute (which I would try to avoid because it is less readable):

    $("select[name='d1'], select[name='d2']").change(function(){xxx});
    
like image 195
PleaseStand Avatar answered Oct 20 '22 00:10

PleaseStand


$("select[name='d1'],select[name='d2']").change(function(){xxx});
like image 44
ryanlahue Avatar answered Oct 20 '22 00:10

ryanlahue


You could select d1 or d2:

$("select[name='d1'], select[name='d2']").change(function(){xxx});

But it would be more elegant if you assigned them the same class and then selected based on that:

<select name="d1" class="d">...</select>
<select name="d2" class="d">...</select>

$("select.d").change(function(){xxx});
like image 40
John Kugelman Avatar answered Oct 20 '22 01:10

John Kugelman