【发布时间】:2021-05-31 00:54:37
【问题描述】:
我尝试了几种不同的方法,但似乎都不适合我。在我解释一下之后,我将输入我尝试过的方法。 我有一个 ID 为“commentform”的表单,我想在页面重新加载并且用户没有单击提交按钮时自动提交用户输入。提交按钮的 id 为“提交”。表单的 textarea 字段是“评论”(我认为这不相关。
尝试 1: 结果:访问时自动重新加载页面,填写文本表单时出错。
<script type="text/javascript">
jQuery(window).on('load', function() {
jQuery('#submit').click();
});
</script>
尝试 2: 结果:什么都没有发生。
<script type="text/javascript">
window.onbeforeunload = function () {
jQuery('#submit').click();
});
</script>
尝试 3: 结果:什么都没有发生。
<script type="text/javascript">
window.onload = function () {
jQuery('#commentform').click();
});
</script>
尝试 4: 结果:什么都没有发生。
<script type="text/javascript">
jQuery(window).on('beforeunload', function() {
jQuery('#commentform').click();
});
</script>
尝试 5: 结果:什么都没有发生。
<body onbeforeunload ='checkRequest(event)'>
//form is here
<script type="text/javascript">
function checkRequest(event){
var __type= event.currentTarget.performance.navigation.type;
if(__type === 1 || __type === 0){
document.getElementById('commentform').submit();
}
}
</script>
</body>
尝试 6: 结果:没有
<script type="text/javascript">
window.onbeforeunload = refreshCode;
function refreshCode(){
document.getElementById("commentform").submit();
return null;
}
</script>
尝试 7: 结果:没有
<script type="text/javascript">
window.onload = function(){
document.forms['commentform'].submit();
}
</script>
更新
如果重要的话;表单没有硬编码到刷新的页面上。它在另一个文件中,并使用<?php timed_comment_template( '/short-comments-timed.php' ); ?> 调用
我还通过注释掉该调用并将其硬编码到页面中来尝试这些解决方案,但收到相同的结果。
表格:
<form id="commentform" action="<?php echo esc_url(get_option('siteurl')); ?>/timed-comments-post.php" method="post">
<div id="form-section-comment" class="form-section">
<div class="form-textarea">
<textarea id="comment" name="comment" cols="45" rows="8" tabindex="6"></textarea>
//another javascript is here for counting words.
</div>
</div>
<div class="form-submit"><input id="submit" name="submit" class="button-small button-green" type="submit" value="Submit" tabindex="7" /><input type="hidden" name="comment_post_ID" value="<?php echo esc_attr($id); ?>" /></div>
<?php comment_id_fields(); ?>
</form>
//problem javascript is going here.
【问题讨论】:
标签: javascript