我有此调查存储到本地存储。单击“提交”后,系统会提示用户“确定”。我在用户点击“确定”后尝试导航到我的目录中的确认HTML页面(Confirmation.html)。但我无法同时实现存储值和导航。看来,只能得到任何一个。任何帮助将不胜感激。
function clicked() {
if (confirm('Are you sure you want to submit? You will not be able to go back.')) {
form.submit();
} else {
return false;
}
}
$('form').submit(function () {
var person = $("#FirstName").val() + "." + $('#LastName').val();
$('input, select, textarea').each(function () {
var value = $(this).val(),
name = $(this).attr('name');
localStorage[person + "." + name] = value;
window.location.href = "Confirmation.html";
console.log('stored key: ' + name + ' stored value: ' + value);
});
});
<button type="submit" value="Save" id="Save" onclick="clicked();" >Submit Form</button>
如果以上内容没有显示我的问题,请点击 jsfiddle
答案 0 :(得分:0)
试试这个
<script>
$( document ).ready(function() {
$('#Save').click(function (e) {
if (confirm('Are you sure you want to submit? You will not be able to go back.')) {
var person = $("#FirstName").val() + "." + $('#LastName').val();
$('input, select, textarea').each(function () {
var value = $(this).val(),
name = $(this).attr('name');
localStorage[person + "." + name] = value;
window.location.href = "Confirmation.html";
console.log('stored key: ' + name + ' stored value: ' + value);
});
}
});
});
</script>
并从按钮中删除onclick="clicked();"
。
答案 1 :(得分:0)
我不确定您为什么需要 confirmation.html 页面。请考虑我的意见如下:
第一名:您已经要求用户确认给他一个消息框。此后,您只应将表单数据(在任何其他验证之后)提交到服务器端页面(我相信您已在action
的{{1}}值中提及)
第二名:如果您仍然需要 confirmation.html 页面,那么您应该从服务器重定向到 confirmation.html - 侧页,但不是来自您的表单页面(当前页面)。现在,取决于 confirmation.html 的使用情况,您应该在将表单数据提供到数据库之前/之后(或执行其他操作)重定向到 confirmation.html 。< / p>