我想呼籲一下形式的一個按鈕jQuery的功能,在這裏調用一個jQuery查詢是我使用上點擊一個按鈕
按鈕代碼代碼:
<input type="submit" src="images/submitbutton.png" alt="Submit button" name="submit_button" class="submit_button" id="submit_button">
形式出發代碼
<form method="post" action="" id="contactForm">
jQuery代碼
<script type="text/javascript">
$(document).ready(function() {
//if submit button is clicked
$('input[name=submit_button]').submit(function() {
alert("testing")
.....
我試過所有的提交按鈕來調用jquery函數但無濟於事(我嘗試使用submit()或click())。
什麼可能我是做錯了(此表格位於HTML頁)
編輯:剛插入的jQuery函數我試圖運行的代碼,我實際上是想利用調用PHP腳本AJAX,然後隱藏表單如果表單提交成功
$(document).ready(function() {
//if submit button is clicked
$('#submit_button').submit(function() {
alert("testing") /*get the email value*/
var name = $('input[name=name]');
var email = $('input[name=email]');
var subject= $('input[name=subject]');
var message = $('textarea[name=message]');
data = 'name=' + name.val() + '&email=' + email.val() + '&subject=' +
subject.val() + '&message=' + encodeURIComponent(message.val());
//disabled all the text fields
$('.text').attr('disabled','true');
//start the ajax
$.ajax({
//this is the php file that processes the data and send mail
url: "mai.php",
//GET method is used
type: "POST",
//pass the data
data: data,
//success
success: function() {
$('.contact_form').fadeOut('slow');
//show the success message
$('.simple-sucess').fadeIn('slow');
//if process.php returned 0/false (send mail failed)
}
});
//cancel the submit button default behaviours
return false;
});
});
</script>
相關:http://stackoverflow.com/questions/5411198/on-click-with-button-in-jquery – 2012-01-27 01:53:58