2012-09-26 58 views
-4

請讓我知道這段代碼有什麼問題。它沒有顯示警報。jquery的ready函數

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 
    <script> 
     $(document).ready(function() { 
      $("button").click(function() { 
      alert("hi"); 
     }); 
    }); 
    </script> 

<form id="theform"> 
    <input type="text" name="dt" id="id" value="" /> 
    <input type="button" name="submit" value="submit" /> 
</form> 

回答

2
$("button") 

應該

$("input[type=button]") OR  $("input[name=submit]") 

檢查FIDDLE

的選擇是不是擺在首位正確的..

$('#btn') // If id of the button is btn 

$('.btn') // If class of the button is btn 
1

你的html中沒有'button'類型的標籤。它是輸入標籤。請把它改寫爲:

$(document).ready(function() { 
    $("input[type=button]").click(function() { 
    alert("hi"); 
    }); 
}); 
​ 
+3

There is _is_ a'

+1

我的意思是他的標記中沒有按鈕類型的標記。 – HungryCoder

1

正確實施這個方法與提交按鈕上的點擊操作無關。這是爲了與表單上的提交操作掛鉤。像這樣:

$(document).ready(function() { 
    $("#theform").submit(function() { 
    alert("hi"); 
    }); 
}); 
相關問題