仅当验证表单时,按钮onclick功能

时间:2015-03-06 17:06:42

标签: c# jquery html ajax asp.net-mvc-4

我在局部视图中有一个表单,我想在点击一个按钮时发生2个功能,但只有在验证时才有表格。当我将onclick函数添加到按钮时,表单不再验证。我可以在按钮中设置是否(有效)。可以这样做吗?我想要的只是在表格被验证时调用函数。

              <input type="submit" value="Create" id="btnclick"  onclick="CreateMech(); createUser(document.getElementById('username').value, document.getElementById('password').value)"   class="btn-danger"  />

1 个答案:

答案 0 :(得分:0)

如果验证失败,您可以使用prevent default来停止点击。

之类的东西
$(document).on('click', '#btnclick', function(e){
    if("validation check function here" == false){
        e.preventDefault();
        alert("validation failed);
    }else{
        CreateMech(); //adding the else here will make the on click on your button unnecessary
    }
});

应该适合你