Asp.net mvc 如何检查按下了哪个提交按钮?
我为表单提交设置了两个提交按钮。 我想知道在没有对每个按钮应用.click()事件的情况下单击了哪个提交按钮。 还有一件事,我正在使用ASP.NET MVC 3,希望在控制器中获得单击按钮的名称 以下是设置:Asp.net mvc 如何检查按下了哪个提交按钮?,asp.net-mvc,jquery,Asp.net Mvc,Jquery,我为表单提交设置了两个提交按钮。 我想知道在没有对每个按钮应用.click()事件的情况下单击了哪个提交按钮。 还有一件事,我正在使用ASP.NET MVC 3,希望在控制器中获得单击按钮的名称 以下是设置: <a class="anchorbutton"> <input type="submit" value="Save" id="Save" class="hrbutton"/> </a> <input type="submit" value=
<a class="anchorbutton">
<input type="submit" value="Save" id="Save" class="hrbutton"/>
</a>
<input type="submit" value="Save & Next" id="SaveNext" class="hrbutton anchorbutton"/>
$('#form').live('submit', function (e)
{
e.preventDefault();
var form = $('#form');
$.ajax({
cache: false,
async: true,
type: "POST",
url: form.attr('action'),
data: form.serialize(),
success: function (data) {
$.ajax({
url: 'url',
dataType: "html",
success: function (data) {
$("#div").html(data);
}
});
}
});
return false;
});
[HttpPost]
public JsonResult Post(FormCollection form, string submitButton)
{
}
$('#form').live('submit',函数(e)
{
e、 预防默认值();
变量形式=$('形式');
$.ajax({
cache:false,
async:true,
类型:“POST”,
url:form.attr('action'),
数据:form.serialize(),
成功:功能(数据){
$.ajax({
url:'url',
数据类型:“html”,
成功:功能(数据){
$(“#div”).html(数据);
}
});
}
});
返回false;
});
[HttpPost]
公共JsonResult Post(FormCollection表单、字符串提交按钮)
{
}
试试看
试一试
请给出您的按钮名称:
<a class="anchorbutton">
<button type="submit" name="save" id="Save" class="hrbutton">Save</button>
</a>
<button type="submit" name="savenext" id="SaveNext" class="hrbutton anchorbutton">Save & Next</button>
哦,顺便说一句,
.live
jQuery函数已经被弃用了。请改用.on
。指定按钮名称:
<a class="anchorbutton">
<button type="submit" name="save" id="Save" class="hrbutton">Save</button>
</a>
<button type="submit" name="savenext" id="SaveNext" class="hrbutton anchorbutton">Save & Next</button>
哦,顺便说一句,
.live
jQuery函数已经被弃用了。请改用上的。尝试将click事件处理程序绑定到Save和SaveAndNext按钮,如
$(document).delegate("#Save,#SaveAndNext","click",function(e){
console.log($(this).attr("id"));//here you will know which button was pressed
//next you can check if the form is valid or not
if($(this).closest('form').valid()){
//form is valid submit it ajax(ily)
}else{
//form is not valid
}
});
还可以缓存选择器
var $this = $(this);
我们可以尝试将click事件处理程序绑定到Save和SaveAndNext按钮,如
$(document).delegate("#Save,#SaveAndNext","click",function(e){
console.log($(this).attr("id"));//here you will know which button was pressed
//next you can check if the form is valid or not
if($(this).closest('form').valid()){
//form is valid submit it ajax(ily)
}else{
//form is not valid
}
});
还可以缓存选择器
var $this = $(this);
谢谢你的回复。我在问题中提到我不想使用click event。谢谢回复。我在问题中提到我不想使用click event.$(“#form”).submit(函数(事件){});我在控制器中获得了按钮名称,但验证无效。$(“#表单”).submit(函数(事件){});我在控制器中获得了按钮名称,但验证无效。您使用的是哪个jquery版本我使用的是jquery 1.5.1您使用的是哪个jquery版本我使用的是jquery 1.5.1