简体   繁体   English

AJAX不更新文本框的值

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

I want to update a value of a input text box via AJAX. 我想通过AJAX更新输入文本框的值。 I cannot make it work althouth almost the same AJAX code works fine for 'text' only. 我无法让它工作,几乎相同的AJAX代码只适用于'text'。 The same behavior for ff & chrome. ff和chrome的行为相同。

the only difference is 唯一的区别是

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';" 

sample code: 示例代码:

<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')">

and the function 和功能

    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();
    } 

the AJAX code doesn't update the value of the text box. AJAX代码不会更新文本框的值。 The inline function does. 内联函数可以。

But for text (not the input text box) even AJAX works fine. 但对于文本(不是输入文本框),即使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')">

and the function 和功能

    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();
    }

I wasn't able to reproduce your error. 我无法重现您的错误。 Check out http://jsbin.com/ahusa4/3/edit 查看http://jsbin.com/ahusa4/3/edit

Are you sure you don't have another element with id "f1"? 你确定你没有另一个id为“f1”的元素吗? maybe the common: <form id='f1' name='f1'> 也许是常见的: <form id='f1' name='f1'>

Have you checked the JavaScript console for errors? 您是否检查过JavaScript控制台是否有错误? Is it possible you are getting an Access-Control-Allow-Origin error when the script is executing the AJAX call? 当脚本执行AJAX调用时,您是否可能收到Access-Control-Allow-Origin错误?

If the web page you are running the XMLHttpRequest from is not on the same domain as the URL of the AJAX request then the browser is stopping the request. 如果您运行XMLHttpRequest的网页与AJAX请求的URL不在同一个域中,则浏览器将停止该请求。 Cross-site HTTP requests are restricted. 跨站点HTTP请求受到限制。

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

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