【问题标题】:How to pass input values to ajax如何将输入值传递给ajax
【发布时间】:2015-11-19 16:48:09
【问题描述】:

好的,我是 Ajax 的新手。我的问题是我不确定如何检索 <input> 标签中的数据并将其发送到 Ajax。我已经尝试在互联网上搜索,但大多数解决方案都是使用 jQuery Ajax,这是我目前不寻找的。​​p>

这是我的代码。

我想保存这个值,以便我的 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

【问题讨论】:

  • 好吧,但是您正在尝试使用 jQuery 来获取输入字段值。你在它前面放了一个点,但你需要一个散列。 (var ID = $("#IDValue").val();)
  • var ID = document.querySelector('#IDValue').value; 是非 jquery 等价物。
  • @akhilp2255 我不认为它是重复的......那个页面使用的是 jquery,这不是我想要的。

标签: javascript php html ajax


【解决方案1】:

替换它

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

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

【讨论】:

  • 谢谢!但是如果是&lt;input class="IDValue" name="IDValue" value="&lt;?php echo $row['exist']?&gt;" &gt;(如果是类?)
  • 对于类使用这个 document.getElementsByClassName('IDValue');
  • 哈哈是的!我刚刚测试并解决了我的问题!谢谢!
【解决方案2】:

我对您的 $row['exist'] 是否返回值以及您用于 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-18
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2017-05-29
    • 1970-01-01
    相关资源
    最近更新 更多