HTML: JavaScript: 阻止表单提交和调用 Javascript 函数 [英] HTML: JavaScript: Block Form submission and call Javascript function

查看:26
本文介绍了HTML: JavaScript: 阻止表单提交和调用 Javascript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按下表单中的提交按钮时进行 AJAX 调用.事实上,我无法删除

因为我也想进行客户端验证.我试过这个代码.

I want to make AJAX call when a submit button in the form pressed. InFact I cant remove the <form> because I want to made clientside validation also. I tried this code.

<form name="search" >
Name: <input type="text" name="name1"/>
Age: <input type="text" name="age1"/>
<input type="submit" name="Submit" value="Submit" onclick="makeSearch()"/>
</form>

JS

function makeSearch(){
alert("Code to make AJAX Call");
}

使用此代码后,alert() 未显示,但页面已重新加载.我想阻止页面重新加载并调用JS函数.

After using this code alert() not showing but the page is reloaded. I want to block the page reload and call the JS function.

谢谢

推荐答案

onsubmit 属性添加到 form 标签:

Add the onsubmit attribute to the form tag:

<form name="search" onsubmit="return makeSearch()" >
  Name: <input type="text" name="name1"/>
  Age: <input type="text" name="age1"/>
  <input type="submit" name="Submit" value="Submit"/>
</form>

并且javascript在最后添加return false:

And javascript add return false at the end:

function makeSearch() {
  alert("Code to make AJAX Call");
  return false;
}

这篇关于HTML: JavaScript: 阻止表单提交和调用 Javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆