javascript 如何禁用提交操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10378876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable submit action
提问by Fadamie
Hi i have have this form that i do no want to perform an action when the submit button is clicked. All i want to do is perform the a function that loads data into a div. Any Ideas??
嗨,我有这个表单,当单击提交按钮时,我不想执行任何操作。我想要做的就是执行一个将数据加载到 div 的函数。有任何想法吗??
<form method="POST" action="" id="search-form">
<input type="text" name="keywords" />
<input type="submit" value="Search" id="sButton" onclick="loadXMLDoc('file.xml')" />
</form>
回答by Alon Gubkin
onclick="loadXMLDoc('file.xml'); return false;"
or even better:
甚至更好:
<script>
window.onload = function() {
document.getElementById("search-form").onsubmit = function() {
loadXMLDoc('file.xml');
return false;
};
};
</script>
To implement loadXMLDoc, you can use the ajax module in jQuery. for example:
要实现 loadXMLDoc,您可以使用 jQuery 中的 ajax 模块。例如:
function loadXMLDoc() {
$("div").load("file.xml");
}
Final code using jQuery:
使用 jQuery 的最终代码:
<script>
$(function() {
$("#search-form").submit(function() {
$("div").load("file.xml");
return false;
});
});
</script>
回答by Dhamu
I think you need ajax function to load data with in div
without page reload
我认为您需要 ajax 函数来加载数据而div
无需重新加载页面
Change input type submit
to button
将输入类型更改submit
为button
<input type="button" value="Search" id="sButton" onclick="AjaxSend()" />
Ajax CAll:
Ajax 调用:
<script type="text/javascript">
function AjaxSend(){
$.get('file.xml', function(data) {
$('div').html(data);
});
}
</script>
回答by Bathri Nathan
use prevent defaults to avoid form action.please refer the code below it might help you
使用阻止默认值来避免表单操作。请参考下面的代码,它可能对您有所帮助
function createRecord(){
event.preventDefault();
}
<form>
<input type="text"/>
<input type="submit" onclick="createRecord()"/>
</form>