2010-05-07 8 views
20

का उपयोग करके क्लिक किया जाता है कि क्या मैं सिर्फ उत्सुक हूँ अगर वहाँ इन पंक्तियों के साथ कुछ करने के लिए एक आसान तरीका है:कैसे जांच करने के लिए एक बटन जावास्क्रिप्ट

जावास्क्रिप्ट:

if(document.getElementById('button').clicked == true) 
{ 
    alert("button was clicked"); 
} 

एचटीएमएल:

<input id="button" type="submit" name="button" value="enter"/> 

उत्तर

47

आप इस के लिए एक क्लिक ईवेंट हैंडलर जोड़ सकते हैं:

document.getElementById('button').onclick = function() { 
    alert("button was clicked"); 
}​;​ 

यह क्लिक होने पर चेतावनी देगा, अगर आप इसे बाद में ट्रैक करना चाहते हैं, तो केवल उस चर में एक वैरिएबल को सचेत करने के बजाय, या variable++ सेट करें, यदि आप क्लिक की संख्या गिनना चाहते हैं, जो भी आपका अंतिम उपयोग है। You can see an example here

2

बस ऑनक्लिक ईवेंट ऊपर हुक:

<input id="button" type="submit" name="button" value="enter" onclick="myFunction();"/> 
+10

कृपया इन-लाइन संचालकों का उपयोग नहीं करते, [बहुत सारे हैं ऐसा करने के कारण नहीं] [http://stackoverflow.com/questions/2479557/why-is-it-bad-practice-to-use-links-with-the-javascript-protocol/2479582#2479582)। –

2

यह कर देगा

<input id="button" type="submit" name="button" onclick="myFunction();" value="enter"/> 

<script> 
function myFunction(){ 

alert("You button was pressed"); 

}; 

</script> 

आशा में मदद करता है कि

संबंधित मुद्दे