如何将输入值传递给ajax
问题描述:
好吧,我是新来的Ajax。我的问题是我不确定如何检索<input>
标记中的数据并将其发送到Ajax。我试图在互联网上搜索,但大多数解决方案都使用jQuery Ajax,这是我目前没有在寻找的。如何将输入值传递给ajax
这是我的代码。
我要保存这个值,这样我的Ajax可以读取它...
<input id="IDValue" name="IDValue" value="<?php echo $row['exist']?>" >
这是我的Ajax脚本...
function message(){
var ID=$(".IDValue").val();
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("POST","retrieveMsg.php?q=" +ID,true);
xmlhttp.send();
}
请帮帮我,伙计们。我做这个方法的原因是(我以前的帖子)Send input value to php using ajax with result printed to div
答
我感到困惑的$行[“存在”]返回值或者不是,以及用于id =“txtHint”的html控件。在这里,我提供的演示其同某种方式对你的代码...尝试和有一个想法,并进行更改按您的要求......
<html>
<head>
<script src="jquery.js"></script>
</head>
<body>
<input id="IDValue" name="IDValue" value="<?php echo 'hello';?>" >
<textarea id="txtHint"></textarea>
<input type="button" value="Click" onClick="message()"/>
<script>
function message(){
var ID=$("#IDValue").val();
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("POST","login.php?q=" +ID,true);
xmlhttp.send();
}
</script>
</body>
</html>
好吧,不过你要使用jQuery来获得输入字段值。你正在把它放在面前,但你需要一个散列。 (var ID = $(“#IDValue”)。val();) – KiwiJuicer
'var ID = document.querySelector('#IDValue')。value;'是非jquery的等价物。 – Shilly
查看https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Submitting_forms_and_uploading_files。 –