方式一: 使用input type="submit" 提交

<form action="http://www.w3school.com.cn/tiy/loadtext.asp" onsubmit="return false;">
    <input type="text" name="userName" />
    <input type="submit" />
</form>

onsubmit 事件会在表单中的确认按钮被点击时发生。  return false会阻止提交;

 

方式二:使用js提交 submit()方法

<form action="http://www.w3school.com.cn/tiy/loadtext.asp" id="myForm">
    <input type="text" name="userName"/>
    <input type="button" onclick="submitForm();" />
</form>
function submitForm() {
    document.getElementById("myForm").submit();
}

 

方式三: 使用jquery提交 submit()方法

<form action="http://www.w3school.com.cn/tiy/loadtext.asp" name="testForm">
    <input type="text" name="userName" />
    <input type="submit" />
</form>
$("form[name=testForm]").submit(function(e) {
    // 阻止默认提交
    //e.preventDefault();
        // 或者
    //return false;
});

使用 e.preventDefault(); 或者 return false; 可以阻止默认提交

 

方式四: 使用ajax提交(或者jquery封装的ajax 推荐)

参考地址:http://www.w3school.com.cn/jquery/ajax_post.asp

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

 

相关文章:

  • 2021-10-28
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-25
  • 2021-09-13
相关资源
相似解决方案