2012-01-27 206 views
1

我想呼吁一下形式的一个按钮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> 
+0

相关:http://stackoverflow.com/questions/5411198/on-click-with-button-in-jquery – 2012-01-27 01:53:58

回答

2

好提供一个成功的消息,该名称应该是在引号:

$('input[name="submit_button"]') 

http://api.jquery.com/attribute-equals-selector/

value [..]属性值。报价是强制性的。

不管怎样,您应该可能使用ID。我不认为你打算在按钮上使用.submit。尝试使用.click代替,或在表单上使用.submit

+0

此外,您可能需要返回true/false取决于你打算如何处理onsubmit处理程序的默认行为 – thescientist 2012-01-27 01:53:43

+0

我试过使用,但没有用,代码是$(#submit_button).submit(function() – 2012-01-27 01:53:58

+0

您的意思是$('#submit_button')'? – 2012-01-27 01:54:38

0

你有没有尝试过这样的代码:

<script type="text/javascript"> 

    $(document).ready(function() { 
     //if submit button is clicked 
     $('#submit_button').submit(function() {  
     alert("testing"); 
     }); 
    }); 

</script> 
+0

,谢谢我试过了,但它只是刷新页面 – 2012-01-27 02:22:48

+0

您的目标是提交表单输入按钮? – jeff 2012-01-27 02:31:21

+0

我刚刚更新了问题,以显示我实际上正在尝试做什么,我试图在jQuery中调用一个将提交表单数据到php邮件表单的ajax函数。邮件表单工作正常..只是这个函数这是驱使我坚果 – 2012-01-27 02:36:40

0

你为什么不尝试使用类型的按钮,而不是提交?如果您想在单击按钮后刷新页面,则可以在脚本结尾处添加返回true。 =)