2013-09-10 37 views
0

我有一個表單,其中有一些文本輸入和一個提交按鈕。提交時,我想觸發一個功能,但我遇到了困難。表單按鈕提交的觸發函數

形式將在下面,我已經試過如下:

$('#contactForm input.btn_submit').click(function(event){ // function here 

$('#contactForm input.btn_submit').click(function(event){ // function here 

$('#contactForm.report_row.input.btn_submit').click(function(event){ // function here 

,但沒有運氣。

這裏是形式:

<form action="https://example.com/index.php/contact" method="post" id="contactForm" name="contactForm"> 
    <input type="hidden" /> 

    <div class="report_row"> 
     <strong>Your Name:</strong><br /> 
     <input type="text" id="contact_name" name="contact_name" value="" class="text" />    </div> 
    <div class="report_row"> 
     <strong>Your Email Address:</strong><br /> 
     <input type="text" id="contact_email" name="contact_email" value="" class="text" />    </div> 
    <div class="report_row"> 
     <strong>Your Phone Number:</strong><br /> 
     <input type="text" id="contact_phone" name="contact_phone" value="" class="text" />    </div> 
    <div class="report_row"> 
     <strong>Message Subject:</strong><br /> 
     <input type="text" id="contact_subject" name="contact_subject" value="" class="text" />    </div>        
    <div class="report_row"> 
     <strong>Message:</strong><br /> 
     <textarea id="contact_message" name="contact_message" rows="4" cols="40" class="textarea long" ></textarea>    </div>  
    <div class="report_row"> 
     <strong>Security Code:</strong><br /> 
     20 + 9 = <br /> 
     <input type="text" id="captcha" name="captcha" value="" class="text" />    </div> 
    <div class="report_row"> 
     <input name="submit" type="submit" value="Send Message" class="btn_submit" /> 
    </div> 
</form> 

我怎麼會觸發有人點擊在年底提交按鈕的功能?

+0

你是否包括jQuery的?你是否將你的jQuery封裝在一個文檔準備好的調用中? – j08691

+0

不知道這是否是你的問題中的拼寫錯誤,但選擇器似乎沒有它們之間的空間..即$'('#contactForm .report_row input.btn_submit')'或只是簡單地'$('#contactForm' ).find('。btn_submit')。' – PSL

+1

但是你可以試試'$('#contactForm.btn_submit')' – Praveen

回答

1

以下將觸發任何時候提交表單:

$(document).ready(function() { 
    $('#contactForm').submit(function() { 
     //... your code here 
    }); 
}); 
1

怎麼樣

$('form').submit(function(){// ... 
            }); 
1

也許你可以嘗試做這樣的事情:

$('#contactForm').submit(function() { 

}); 

$('#contactForm').on('submit', function() { 

}); 
2

不清楚你的意思是「我有困難」。

這可能是你不停止表單提交,所以你可以運行你的功能。在這種情況下,您需要使用.preventDefault()。我建議你也將你的函數封裝在一個準備就緒的函數中,這樣你的代碼將在頁面加載完所有的html時運行。

喜歡:

$('#contactForm input.btn_submit').click(function(event){ 
    event.preventDefault(); 
    // rest of code 
    alert('Working!'); 
}); 

演示here