gpt4 book ai didi

JavaScript 更新页面而不是更新字段

转载 作者:行者123 更新时间:2023-12-03 06:11:19 24 4
gpt4 key购买 nike

原始页面网址为http://localhost:8080/appname/help.action该页面包含一个表单

<div id="message"></div>

<form id="help_form" name="help_form" onsubmit="return validate_help_form()">
...
<input type="submit" value="Submit" />
</form>

脚本

function validate_help_form() {

if ( ... ) {
...
return false;
}

$.ajax({

url : 'http://localhost:8080/appname/question',
type : 'POST',
dataType : 'html',
success : function(result) {
console.log('^^^^^^^^^');
$('#message').text(result);
},
error : function(xhr, ajaxOptions, thrownError) {
alert('Unable to receive result. ' + thrownError);
},
});

}

servlet 相当简单

logger.info("-----------");

try {
response.getWriter().append( "a servlet result" );
} catch (IOException e) {
e.printStackTrace();
}

问题是当ajax请求工作时,原始页面再次通过路径` http://localhost:8080/appname/help.action ruqested 。但我想用 ajax 结果更新字段消息。所以在控制台中我得到

^^^^^^^^^
Navigated to http://localhost:8080/my-mates-ajax/help.action?username=...

最佳答案

尝试使用这个

<form onsubmit="event.preventDefault(); return validate_help_form();">

基本上,您需要首先阻止表单提交,然后才能执行该操作。否则,表单将与 ajax 请求一起提交。

关于JavaScript 更新页面而不是更新字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39304189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com