ajax异步请求的一个jscript
<form action="" method="post" enctype="multipart/form-data"><input name="q" id="txt" class="txt" type="text" />
<input type="submit" class="btn" name="submit" value="Upload" /><br />
</form>
异步请求的表单,怎么让他点击按钮把name="q"的值传到test.php,然后回传请求数据<divid="txtHint" class="file-box">ajax所请求的数据显示位置</div> 本帖最后由 没事玩玩破解 于 2020-7-24 20:18 编辑
var submit = document.querySelector('input');
submit.onclick = function () {
var txt = document.getElementById('txt');
var txtHint = document.getElementById('txtHint');
var xhr = new XMLHttpRequest();
xhr.open('post','test.php',true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200){
var response = JSON.parse(xhr.responseText);
txtHint.innerText = response.name;
}
};
xhr.send('name='+ txt.value);
};
页:
[1]