我尝试通过以下代码来防止默认表单提交,但它不起作用。这有什么问题。
<div class="section-register">
<form id="registerform" novalidate="novalidate" method="post" action="http://localhost/yify2/wp-login.php?action=register" name="registerform">
<input id="wp-submit" class="button button-primary button-large" type="submit" value="Register" name="wp-submit">
</form>
</div>
我的脚本是:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".section-register").find("#registerform").submit(function(event){
if(whatever) {
event.preventDefault();
}
});
});
</script>
我还尝试了以下代码:
<script>
jQuery(document).ready(function() {
jQuery("#registerform").submit( function(e) {
e.preventDefault();
});
});
</script>
最佳答案
如果您的表单是通过 AJAX 请求加载的,则需要在将表单添加到 DOM 后绑定(bind)事件,或者将其绑定(bind)在静态节点上:
委托(delegate)事件绑定(bind):
<script>
jQuery(function($) { // Short for jQuery(document).ready(function() {
// Bind the event to a static node (here I use document).
// The closer to the dynamic node the better.
// From your code it looks like you might be able to use
// $('.section-register') as the static node.
$(document).on('submit', '#registerform', function(event) {
event.preventDefault();
});
});
</script>
关于javascript - Jquery 防止默认值在 WordPress 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30666857/