用jquery.form實現多個submit按鈕提交
相關前端程式碼如下:
<form id="from1" action="/AjaxTestAutoAction/submit.cspx" method="post">
<p><span>Input:</span>
<input type="text" name="input" style="width: 300px" value="aijava"/></p>
<p><span>Output:</span>
<input type="text" id="output" style="width:300px"readonly="readonly"/></p>
<input type="submit" name="Base64" value="轉換成Base64編碼"/>
<input type="submit" name="md5" value="計算md5"/>&nvsp;
<input type="submit" name="sha1" value="計算sha1"/>
</form>
<script type="text/javascript">
$(function(){
$("#form1").ajaxForm(function(result)){
$("#output").val(result);
});
});
</script>
服務端程式碼:
public class AjaxTestAutoAction{
[Action]
public string Base64(string input){
return Convert.ToBase64String(Encoding.Default.GetBytes(input));
}
[Action]
public string md5(string input){
byte[ ] bb=Encoding.Default.GetBytes(input);
byte[ ]md5=(new MD5CryptoServiceProvider()).ComputeHash(bb);
return BitConverter.ToString(md5).Replace("-",string.Empty);
}
[Action]
public string Sha1(string input){
byte[ ] bb=Encoding.Default.GetBytes(input);
byte[ ] sha1=(new SHA1CryptoServiceProvider()).ComputeHash(bb);
return BitConverter.ToString(sha1).Replace("-",string.Empty);
} }
伺服器定義三個方法,對應三個submit按鈕
前端還是隻呼叫一個ajaxForm解決所有問題
這種方法就是由前端的jQuery,jQuery.form以及服務端的MYMVC框架共同實現的。