2013-01-15 24 views
0

我使用下面的HTML:如何點擊DIV使用javascript提交表單?

<form action="/User/Account/LogOff" id="logoutForm" method="post"> 
    <button class="float-left submit-button" title="Logoff">Logoff</button> 
</form> 

我想,以取代本:

<form action="/User/Account/LogOff" id="logoutForm" method="post"> 
    <div class="button" id="logout" title="Logout"> 
     <span>Logout</span> 
    </div> 
</form> 

有沒有一種方式,我可以趕上在div#註銷點擊,並以此作爲提交表單?我看到這個鏈接:

<a href="javascript:document.getElementById('logoutForm').submit()">Logout</a> 

但我怎麼能這樣做,像我這樣的DIV?您指出 感謝:

+0

只使用一個提交按鈕,如果你不使用任何客戶端驗證。那的好不要使用不必要的JS,爲您註銷甚至dosent想要的形式。它能成爲一個鏈接 –

+0

你可以試試這個鏈接的解決方案http://stackoverflow.com/questions/6366087/submit-data-on-div-click – Surabhi

回答

1

添加這個到您的DIV:

<div class="button" id="logout" onclick="javascript:document.getElementById('logoutForm').submit();" title="Logout"> 

EDIT 1。

<div class="button" id="logout" onclick="submitForm()" title="Logout"> 

的Javascript:

function submitForm() 
{ 
     document.getElementById('logoutForm').submit(); 
} 
+1

不要把'javascript:'前綴放在'onXXX'屬性中。這隻需要'href'鏈接。 – Barmar