简体   繁体   English

如何将输入值传递给ajax

[英]How to pass input values to ajax

Okay, I'm new to Ajax. 好的,我是Ajax的新手。 My problem is that I'm not sure how to retrieve data which is in the <input> tag and send it to Ajax. 我的问题是我不知道如何检索<input>标签中的数据并将其发送到Ajax。 I have tried searching on the internet, but most of the solutions are using jQuery Ajax, which is what I'm not looking for at the moment. 我试过在互联网上搜索,但大多数解决方案都使用jQuery Ajax,这是我目前不想要的。

Here is my code. 这是我的代码。

I want to save this value so that my Ajax can read it... 我想保存这个值,以便我的Ajax可以读取它...

<input id="IDValue" name="IDValue" value="<?php echo $row['exist']?>" >

This is my Ajax script... 这是我的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();
               }

Please help me, guys. 伙计们,请帮助我。 The reason I am doing this method is because (My previous post) Send input value to php using ajax with result printed to div 我这样做的原因是因为(我以前的帖子) 使用ajax将输入值发送到php,结果打印到div

Replace it 代替它

 var ID=$(".IDValue").val();

With

 var ID = document.getElementById("IDValie").value;

i am confused about your $row['exist'] returns value or not and what html control you used for id="txtHint". 我对你的$ row ['exists']返回值感到困惑,以及你用于id =“txtHint”的html控件。 here i have provided demo which same as your code in some way...try and have an idea and make changes as per your requirement... 在这里,我以某种方式提供了与您的代码相同的演示...尝试并根据您的要求进行更改并进行更改...

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM