按钮不调用post表单与Razor c#

本文关键字:Razor 表单 post 调用 按钮 | 更新日期: 2023-09-27 17:49:16

我有一个页面,当用户点击按钮"Atualizar",而不是调用post,称为JQuery。如何做到这一点?

<form action="/Conselho/Detalhar/6" method="post">    <div class="row">
        <h3>Visualizando Conselho Profissional</h3><hr>
        <input data-val="true" data-val-number="The field ConselhoID must be a number." data-val-required="O campo ConselhoID é obrigatório." id="ConselhoID" name="ConselhoID" type="hidden" value="6">
        <div class="col-md-2">           
            <label for="Sigla">Sigla</label> <span class="field-validation-valid" data-valmsg-for="Sigla" data-valmsg-replace="true"></span><br>    
            <input class="form-control" id="Sigla" name="Sigla" type="text" value="CREA"><br>
        </div>
        <div class="col-md-10">           
            <label for="Descricao">Descrição</label> <span class="field-validation-valid" data-valmsg-for="Descricao" data-valmsg-replace="true"></span><br>    
            <input class="form-control" id="Descricao" name="Descricao" type="text" value="Conselho Federal de Engenharia e Agronomia"><br>
        </div>                
        <div class="col-md-5">        
            <div class="botoes">   
              <input name="btnSubmit" class="btn btn-success" type="submit" value="Salvar">&nbsp;&nbsp;   
              <input name="btnSubmit" class="btn btn-warning" type="submit" value="Excluir">&nbsp;&nbsp;   
              <input name="btnSubmit" class="btn btn-info" type="submit" value="Atualizar">&nbsp;&nbsp;
              <a class="btn btn-default" href="/Conselho/Index">Voltar</a>&nbsp;&nbsp;</div>          
        </div>        
    </div>    
</form>

按钮不调用post表单与Razor c#

    你应该添加一个id属性,这样你就有一个简单的,唯一的按钮选择器。在本例中,我将其命名为"Atualizar"
  1. 为按钮编写javascript事件处理程序
  2. 防止默认行为,在本例中是表单post。
  3. 或者,您可以使用按钮标签,默认情况下,这将不会提交表单

    var doSomething = function(){//你的代码在这里};$ (' # Atualizar ') .click(函数(e) {e.preventDefault ();doSomething ();});

选择:

在javascript中,你可以监听这个特定按钮的click事件,获取这个按钮的表单,序列化表单并将它发送给action方法url。美元(函数(){

  $("#Atualizar").click(function(e){
    e.preventDefault(); //prevent default form submit behaviour
    var _this=$(this);
    var _form=_this.closest("form");
    $.post(_form.attr("action"),_form.serialize(),function(res){
      // do something with res
    });
  });
});