繁体   English   中英

AJAX不更新文本框的值

[英]AJAX doesn't update value of a text box

我想通过AJAX更新输入文本框的值。 我无法让它工作,几乎相同的AJAX代码只适用于'text'。 ff和chrome的行为相同。

唯一的区别是

doesn't work ---> document.getElementById("f1").value=revision;  
works fine -----> document.getElementById("f2").innerHTML=revision;
works fine too -> onClick="javascript:document.getElementById('f1').value+='B';" 

示例代码:

<input type="text" name="f1" id="f1"> 
<input type="button" value="inline function"  onClick="javascript:document.getElementById('f1').value+='B';">
<input type="button" value="AJAX Button" onClick="get_revision2('trunk')">

和功能

    function get_revision2(revision)
    {
      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()
        {
        document.getElementById("f1").value=revision;

        if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
          document.getElementById("f1").value=xmlhttp.responseText;

          }
        }
      xmlhttp.open("GET","http://radek:4567/svn_get_revision?code_base="+revision+"&t=" + Math.random(),true);
      xmlhttp.send();
    } 

AJAX代码不会更新文本框的值。 内联函数可以。

但对于文本(不是输入文本框),即使AJAX工作正常。

<DIV id="f2">revision</div>
<input type="button" value="inline function" onClick="javascript:document.getElementById('f2').innerHTML+='B';">
<input type="button" value="AJAX" onClick="get_revision3('3.0')">

和功能

    function get_revision3(revision)
    {
      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()
        {
        document.getElementById("f2").innerHTML=revision;

        if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
          document.getElementById("f2").innerHTML=xmlhttp.responseText;

          }
        }
      xmlhttp.open("GET","http://radek:4567/svn_get_revision?code_base="+revision+"&t=" + Math.random(),true);
      xmlhttp.send();
    }

我无法重现您的错误。 查看http://jsbin.com/ahusa4/3/edit

你确定你没有另一个id为“f1”的元素吗? 也许是常见的: <form id='f1' name='f1'>

您是否检查过JavaScript控制台是否有错误? 当脚本执行AJAX调用时,您是否可能收到Access-Control-Allow-Origin错误?

如果您运行XMLHttpRequest的网页与AJAX请求的URL不在同一个域中,则浏览器将停止该请求。 跨站点HTTP请求受到限制。

暂无
暂无

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

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