【问题标题】:passing text value to ajax function on clicking submit button单击提交按钮时将文本值传递给 ajax 函数
【发布时间】:2012-05-26 18:28:49
【问题描述】:

我的表单包含一个文本字段 (id="txt1") 和一个提交按钮(id="submit")。在提交按钮的“onclick”事件上调用 Ajax 函数“showhint()”。

<form action="">  
<input type="text" id="txt1"   />
<input type="submit" id="submit" onclick="showhint()"/>
</form>

和ajax函数

    function showhint(){
var xmlhttp;

if(window.XMLHttpRequest)
{
    xmlhttp=new XMLHttpRequest();
}
else
{
    xmllhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("txthint").innerHTML=xmlhttp.responseText;
    }
        }
            xmlhttp.open("GET","gethint.php",true);
            xmlhttp.send();
            }

这里 gethint.php 是 showhint() 函数工作的页面,目标 div 编写 ajax 响应是 "txthint" 。 我想要的是将文本值(表单)传递给 ajax 函数。有什么简单的方法吗? 等待响应,任何有用的响应都将是非常可观的。 提前谢谢

【问题讨论】:

  • 你想通过 ajax 将文本值传递给 php 吗?

标签: php ajax


【解决方案1】:
var value = document.getElementById("txt1").value;

xmlhttp.open("GET","gethint.php?value="+value,true);

在 gethint.php 中

 echo $_REQUEST['value'];

【讨论】:

  • @cowboy:如果 Jusnit 的回答有帮助,请采纳。这就是你对 SO 说“谢谢”的方式。
【解决方案2】:

如果我没看错,你就是在尝试获取文本框的值并将其传递给你的 showhint() 方法。

试试这个:

<input type="submit" id="submit" onclick="showhint(document.getElementById('txt1').value)" value="test"/>

那么你可以接受这样的值:

function showhint(value) {
  // use value here
}

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多