jQuery函数不适用于Form Submit [英] jQuery function not working on Form Submit
本文介绍了jQuery函数不适用于Form Submit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从W3C示例中看到了类似此的内容.在表单提交(警报)上发生操作.但是,当我尝试使用自己的函数此处时,我的操作不会发生(显示隐藏的div) .目标是在使用"GET"请求提交的表单上显示隐藏的div.我是javascript/jQuery/ajax的新手,但是jQuery是否应该在不刷新页面的情况下调用服务器?
I saw from W3C example something like this. An action happens on form submit(an alert). However, when I tried with my own function here, my action doesn't happen (show hidden div). Goal is to show hidden div on form submit using 'GET' request. I am new to javascript/jQuery/ajax, but isn't jQuery supposed make call to server without refreshing page?
不起作用的javascript:
not working javascript:
$(document).ready(function(){
$("form").submit(function showDiv(){
document.getElementById("showMe").style.display = "block";
});
});
无法使用html:
<body>
<form action="" method="GET">
<input type="text" name="LastName" value="Mouse"><br>
<input type="submit" value="Submit">
</form>
<div id="showMe" style="display: none;">
<p>hey this should be hidden</p>
</div>
</body>
推荐答案
您需要取消表单提交在执行代码之前:
You need to cancel the form submit before executing your code:
$(document).ready(function(){
$("form").submit(function(e){
// At this point you'll want to have some type of flag to indicate whether you
// should allow the form submission to continue, or cancel the event. For example,
// it could be whether not showMe is visible (I don't know what you're going for).
e.preventDefault();
$("#showMe").css("display", "block");
// Submit form after your code runs (or whenever you want)
$("form").submit();
});
});
这篇关于jQuery函数不适用于Form Submit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文