AJAX,刷新输入场 [英] AJAX, refresh INPUT field

查看:139
本文介绍了AJAX,刷新输入场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AJAX首次用PHP和MySQL。 我一直关注如何使用AJAX与输入字段的一些例子和教程。

我有2个输入字段的表单。

  • 在第一次输入字段:用户在4位型
  • 在第二个输入字段:4位数抬头的MySQL数据库,并输出在这一领域

大多数的例子是基于div的和跨度的我是对我的第二个输入领域感兴趣的输出而输出。

这怎么可能?

我的js文件:

 函数showUser(STR)
{
  如果(STR ==)
  {
    的document.getElementById(txtHint)的innerHTML =。
    返回;
  }
  如果(window.XMLHtt prequest)
  {
    // $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
    XMLHTTP =新XMLHtt prequest();
  }
  其他
  {
    // code对IE6,IE5
    XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
  }
  xmlhttp.onreadystatechange =功能()
  {
    如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200)
    {
      的document.getElementById(txtHint)的innerHTML = xmlhttp.responseText。
    }
  }
  xmlhttp.open(GET,包括/邮递code.php Q =?+ STR,真正的);
  xmlhttp.send();
}
 

解决方案
  

大多数的例子是基于div的和跨度的我是对我的第二个输入领域感兴趣的输出而输出。

您应该可以下面的例子有两个变化:

  • 使用的。价值,而不是.innerHTML 输入框
  • 将查找您的第二个输入字段 其ID
  

的document.getElementById(secondFieldId)值= xmlhttp.responseText;

I am using AJAX for the first time with PHP and mySQL. I have been following some examples and tutorials on how to use AJAX with INPUT fields.

I have a form with 2 input fields.

  • 1st input field: User type in 4 digits
  • 2nd input field: The 4 digits are looked up in the mySQL db and outputted on this field.

Most examples are based on output in div's and span's while i am interested in output on my 2nd INPUT field.

How is this possible?

My js-file:

function showUser(str)
{
  if (str=="")
  {
    document.getElementById("txtHint").innerHTML="";
    return;
  }
  if (window.XMLHttpRequest)
  {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }
  else
  {
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","include/postalcode.php?q="+str,true);
  xmlhttp.send();
}

解决方案

Most examples are based on output in div's and span's while i am interested in output on my 2nd INPUT field.

You should be able to follow the examples with two changes:

  • use .value instead of .innerHTML for input fields
  • look up your second input field by its ID

document.getElementById("secondFieldId").value=xmlhttp.responseText;

这篇关于AJAX,刷新输入场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆