从重新加载页面

时间:2016-12-03 17:25:59

标签: javascript html

我试图让我的表单提交数据而不重新加载页面本身,但目前我正在努力这样做。

当我在当前设置中单击我的提交按钮时,没有发生任何事情。如果我删除onsubmit="return validateForm()",那么数据将保存,但页面会重新加载。

表单HTML

<form id="predictionform-1" action="" method="post" onsubmit="return validateForm()"><p style="text-align: center;"><input type="hidden" id="_footballpool_wpnonce" name="_footballpool_wpnonce" value="f12119edf4"><input type="hidden" name="_wp_http_referer" value="/play/"><input type="hidden" name="_fp_form_id" value="1"></p><table id="matchinfo-1" class="matchinfo input"><tbody><tr><td class="matchtype" colspan="11">All</td></tr><tr><td class="matchdate" colspan="11">Dec 13, 2016</td></tr><tr id="match-5-1" class="match open" title="match 5">
                    </tr>
                    <tr><td class="score" style="width: 48%; text-align: center;"><input type="text" maxlength="3" name="_home_5" value="3" class="prediction"></td>
<td style="width: 4%; text-align: center;"></td>
                    <td class="score" style="width: 48%; text-align: center;"><input type="text" maxlength="3" name="_away_5" value="1" class="prediction"></td>
                    <td title="score" class="numeric"><span class="no-score"></span></td>
                                            </tr></tbody></table><div class="buttonblock button-matches form-1"><input type="submit" name="_submit" value="Save"></div><input type="hidden" id="_action_1" name="_fp_action" value="update"></form>

的JavaScript

<script type="text/javascript">
function validateForm()
{
    return false;
}

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:1)

不要在提交时使用HTML属性,这不是一个好习惯。

但问题是你必须抓住事件并阻止他的默认行为,如下所示:

var form = document.getElementById("predictionform-1");

function update(){
  console.log("You submited the form!");
}

form.addEventListener("submit", function(event){
  event.preventDefault();
  update();
  
});
<form id="predictionform-1" action="" method="post"><p style="text-align: center;"><input type="hidden" id="_footballpool_wpnonce" name="_footballpool_wpnonce" value="f12119edf4"><input type="hidden" name="_wp_http_referer" value="/play/"><input type="hidden" name="_fp_form_id" value="1"></p><table id="matchinfo-1" class="matchinfo input"><tbody><tr><td class="matchtype" colspan="11">All</td></tr><tr><td class="matchdate" colspan="11">Dec 13, 2016</td></tr><tr id="match-5-1" class="match open" title="match 5">
                    </tr>
                    <tr><td class="score" style="width: 48%; text-align: center;"><input type="text" maxlength="3" name="_home_5" value="3" class="prediction"></td>
<td style="width: 4%; text-align: center;"></td>
                    <td class="score" style="width: 48%; text-align: center;"><input type="text" maxlength="3" name="_away_5" value="1" class="prediction"></td>
                    <td title="score" class="numeric"><span class="no-score"></span></td>
                                            </tr></tbody></table><div class="buttonblock button-matches form-1"><input type="submit" name="_submit" value="Save"></div><input type="hidden" id="_action_1" name="_fp_action" value="update"></form>

上面的表单不会提交,因为沙盒模式不允许表单提交。

相关问题