2009-07-27 13 views
23

के अंदर तत्वों का चयन और फ़िल्टरिंग मुझे एक div के अंदर तत्वों को चुनने और फ़िल्टर करने में समस्या है।jQuery एक div

HTML:

<div id="wrapper"> 
    <input type="text" value="you can edit me"> 
    <input type="button" value="click me"> 
</div> 

jQuery:

$("#wrapper").children().click(function() { 
    alert("hi there"); 
}); 

समस्या मैं हर बार जब मैं div के अंदर कुछ भी क्लिक अलर्ट पाएं है।
लेकिन मेरी आवश्यकता केवल तब चेतावनी देना है जब उपयोगकर्ता बटन पर क्लिक करता है।
मुझे पता है कि jQuery में तत्वों को छानने उपयोग कर रहा है :button

यह है कि मैं क्या करने की कोशिश की है:

$("#wrapper").children(":button").click(function() { 
    alert("hi there"); 
}); 

और

$("#wrapper").children().filter(":button").click(function() { 
    alert("hi there"); 
}); 

यह

किसी को भी पता है कि काम नहीं किया यह कैसे करना है? एक विशिष्ट के लिए

+9

@Erwin - भविष्य के संदर्भ के लिए, इस तरह के प्रश्न पोस्ट करते समय "समुदाय विकी" चेकबॉक्स की जांच से बचें। यह एक वैध प्रोग्रामिंग प्रश्न है और उपयोगकर्ताओं को इससे प्रतिनिधि कमाया जाना चाहिए –

उत्तर

42
$("#wrapper input[type=button]").click(function() { 
    alert("hi there"); 
}); 
1

उपयोग आईडी बटन

<div id="wrapper"> 
    <input type="text" value="you can edit me"> 
    <input type="button" id='btnMyButton' value="click me"> 
    <input type="button" class='btnClass' id='btnMyButton2' value="click me 2"> 
<input type="button" class='btnClass' id='btnMyButton3' value="click me 3"> 
</div> 

$('#btnMyButton').click(function(){ 
alert("hi there"); 
}); 

div में सभी बटन के लिए, जॉन के जवाब का पालन करें। कुछ buttons- के लिए वर्ग का प्रयोग करें

$('.btnClass').click(function(){ 
    alert("all class"); 
}); 

btw, मैं जैसे

$(document).ready(function(){   

}); 
0

तैयार समारोह के अंदर अपने सभी jQuery समारोह रखना चाहते हैं के बारे में कैसे

$("#wrapper :button").click(function() { alert("I love Imogen Poots"); });

देखें this

0

इसे आजमाएं:

$("#wrapper > input[type=button]").click(function() { 
    alert("hi there"); 
});