weixin_33744854 2015-07-20 09:23 采纳率: 0%
浏览 50

Ajax为什么无法显示TextBox的onchange值?

我正试图用Ajax显示TextBox的onchange值。代码如下:

ajaxquery.php

<html>
<head>
<title>Changing textbox value based on dropdown list using Ajax and PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
//fuction to return the xml http object
function getXMLHTTP() { 
    var xmlhttp=false;  
    try{
        xmlhttp=new XMLHttpRequest();
    }
    catch(e)    {       
        try{            
            xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e){
            try{
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e1){
                xmlhttp=false;
            }
        }
    }

    return xmlhttp;
}



function getCurrencyCode(strURL)
{       
var req = getXMLHTTP();     
if (req) 
{
    //function to be called when state is changed
    req.onreadystatechange = function()
    {
        //when state is completed i.e 4
        if (req.readyState == 4) 
        {           
            // only if http status is "OK"
            if (req.status == 200)
            {                       
                document.getElementById('cur_code').value=req.responseText;                     
            } 
            else 
            {
                alert("There was a problem while using XMLHTTP:
" + req.statusText);
            }
        }               
     }          
     req.open("GET", strURL, true);
     req.send(null);
}           
}
 </script>

 <body style="font: 12px Verdana, Arial, Helvetica, sans-serif;">
 <form style="text-align:center" method="post" action="" name="form1">
 <p>Country : <input type="text" name="country"  onChange="getCurrencyCode('code.php?country='+this.value)">
 </form>
 </body>
 </html>

code.php

<?php 
    $country=$_REQUEST['country'];
    echo $country;
?>

值一直不显示,哪里出问题了?

PS:我对ajax几乎一无所知,感谢你的任何帮助:)

  • 写回答

1条回答 默认 最新

  • weixin_33726943 2015-07-20 14:07
    关注
     <p>Country : <input type="text" name="country" onblur="getCurrencyCode('code.php?country='+this.value)">
     <p>Country Code : <input type="text" name="cur_code" id='cur_code' ">
    

    Changed the onchange function to onblur and added another input type. It is working perfectly now!

    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思