2016-02-12 28 views
0

這是我的html代碼:我怎麼可以指定多個選擇的jQuery

<input type="text" id="home"> 
<input type"text" id="dog"> 

我需要做的事情在這些領域中讀取的值。所以我寫了我的jquery代碼:

//this code is wrong because it doesn't work 
$('input[type="text"][id="home"] input[type="text"][id="dog"]').... 

我該如何選擇類似的東西?誰能幫我?

+0

你是什麼意思的「不工作」? – DanM7

回答

2

爲了選擇多個項目中使用逗號之間

<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script> 

    $(document).ready(function() { 

     $('#home, #dog').val("hello"); 
    }); 
</script> 
</head> 

<body> 

<input type="text" id="home"> 
<input type"text" id="dog"> 
</body> 

</html> 
1

當您根據多個條件進行選擇時,在兩個選擇器之間使用逗號。一個簡單的例子是:

$("#element1, #element2, #element3") 

你舉的例子:

$('input[type="text"][id="home"], input[type="text"][id="dog"]') 

Multiple Selector (「selector1, selector2, selectorN」)

1

你應該使用$('#home, #dog');

相關問題