Jquery 如何将提交事件添加到动态生成的表单中?
以下是生成表单的PHP代码: form.phpJquery 如何将提交事件添加到动态生成的表单中?,jquery,Jquery,以下是生成表单的PHP代码: form.php <?php echo ' <form action="/wall/comment.php" method="post" id="submit-comment-form" name="'.$postid.'"> <div class="write-comment-profile-pic"> <img src= "'.$myprofilepic.'" width="30" heig
<?php
echo '
<form action="/wall/comment.php" method="post" id="submit-comment-form" name="'.$postid.'">
<div class="write-comment-profile-pic">
<img src= "'.$myprofilepic.'" width="30" height="30""/>
</div>
<div class="write-comment-textbox-wrap">
<input type="text" class="textbox4" id="write-comment-textbox" value="Write a comment..." name="comment" style="width:65%;" />
</div>
</form>
';
?>
以下是需要提交表单的jquery代码:
$('#image-pop-up').on('submit', '#submit-comment-form', function() {
evt.preventDefault();
});
在index.html中,图像弹出div是空的
只是
这不管用。。。怎么了?如何修复它?我怀疑问题与选择器有关,目标元素要么找不到,要么是正确的目标元素 有几件事可能是错误的:
submit
事件仅在表单上触发,因此您需要确保这正是您的目标evt
的preventDefault()
,但evt
未绑定到事件参数#submit comment form
,那么无论执行时是否有任何元素可用,以下代码都应该有效:
$(document).on('submit', '#submit-comment-form', function(evt){
evt.preventDefault();
})
您的提交在哪里?提交评论表单是否包装在图像弹出窗口中?您的代码暗示了这一点,请显示这两个图像的呈现标记。此图像弹出窗口是一个对话框?@MarkSchultheiss yes#提交评论表单由#图像弹出窗口包装-up@BhushanFirake是的,#图像弹出窗口是一个弹出对话框。。。。在其中,jquery动态生成表单。。。。表单没有提交按钮,它是通过按Enter键提交的。。。但是当我这么做的时候。。。页面加载了注释。phpIf这解决了您的问题,您可以将其标记为答案吗?;)@巴尼+1很好的解释。我知道,但我忘了。谢谢你记住它。
</div>
$(document).on('submit', '#submit-comment-form', function(evt){
evt.preventDefault();
})