我有两个文件与php和html。这是我的A.php文件中的基本信息
<div class="col-md-8">
<input type="email" class="form-control" placeholder="Ingresar Número de Nota" id="idnote" name="idnote">
</div>
<button type="button" class="btn btn-primary" id="notes">Enviar</button>
当用户单击按钮时,它将在js中调用一个函数。
$(document).ready(function() {
var formData = {
'id' : $('input[name=idnote]').val()
};
$.ajax({
type : 'POST',
url : '../consults/findnote.php',
data : formData,
dataType : 'json',
encode : true
})
.done(function(data) {
//Going to findnote.php and keeping formData
//This findnote.php contains a completely different design but I need the first value to make some changes for the user.
})
});
});
问题是在ajax之后,我的网页将不会找到findnote.php,而只是发送值,而我需要显示findnote.php
我知道我是否使用
event.preventDefault();
Ajax将阻止重新加载,但是我没有使用它。
有办法吗?
创建window.location后如何保留值? (因为如果在成功调用后调用文件,则会丢失该值)
我应该只尝试php吗? (它是一个选择)
最佳答案
为什么不使用纯HTML?
<form action="../consults/findnote.php" method="POST">
<div class="col-md-8">
<input type="email" class="form-control" placeholder="Ingresar Número de Nota" id="idnote" name="id">
</div>
<button type="submit" class="btn btn-primary" id="notes">Enviar</button>
</form>
刚刚添加了表单标签并编辑了输入名称,因为它符合您的函数结果。
“问题在于ajax之后,我的网页将不会找到findnote.php,而只是发送值”
这基本上就是ajax的目的:)
使用ajax,可以根据需要将结果附加到当前页面(您不在代码中。仅发送数据)。
上面带有表单标签的另一种解决方案,将结果作为新页面加载,就像经典链接一样。
关于javascript - Ajax,只是发送值而不显示页面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38800632/