2015-07-20 41 views
1

我正在嘗試使用ajax顯示文本框的onchange值。我的代碼如下:Onchange()for AJAX

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:\n" + 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; 
?> 

值不顯示。我哪裏錯了?

P.s.我對ajax完全陌生,對此不瞭解。感謝所有幫助:)

回答

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

改變了平變化功能的onblur,加上另一個輸入類型。它現在正在完美工作!