2012-04-23 94 views
1

我有兩個文本字段,無論用戶在第一個文本字段中輸入什麼字段,Ajax都會顯示第二個字段。我需要使用循環有多行,每一行都有自己的價值。 HTML具有特定值的多個文本字段行使用Ajax

<form style="text-align:center" method="post" action="" name="form1"> 
<?php for($x=0; $x<4; $x++){ ?> 
<input type="text" size="30" id="country" onChange="getCurrencyCode('find_ccode.php?country='+this.value)" /> 
<input type="text" name="cur_code" id="cur_code" ></p> 

阿賈克斯

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) 
    { 
     req.onreadystatechange = function() 
     { 
      if (req.readyState == 4) 
      {   
       if (req.status == 200) 
       {      
        document.getElementById('cur_code').value=req.responseText;      
       } 

      }    
     }   
     req.open("GET", strURL, true); 
     req.send(null); 
    }   
} 

$country=$_REQUEST['country']; 
echo $country; 

請幫我一把。

+0

您是否知道後的文本輸入的內容,改變了「的onChange」事件將不會發出?當輸入失去焦點時它會閃光。 – Exander 2012-04-23 09:11:39

+0

他可以使用'setInterval'來檢查一次,如果當前更改的字段的值確實發生了變化,則啓動該調用。 – Eugene 2012-04-23 09:21:26

回答

1

嘗試像這樣...

<form style="text-align:center" method="post" action="" name="form1"> 
<?php for($x=0; $x<4; $x++){ ?> 
<input type="text" size="30" id="country<?php echo $x; ?>" onChange="getCurrencyCode(<?php echo $x; ?>);" /> 
<input type="text" name="cur_code" id="cur_code<?php echo $x; ?>" ></p> 

javascript函數

function getCurrencyCode(num) 
{  
    thisval = document.getElementById('country'+num).value; 
     if(thisval=="") { 
      alert("Enter Proper Values!..."); 
      return false; 
     } 
    try{ 
     // Opera 8.0+, Firefox, Safari 
     ajaxRequest = new XMLHttpRequest(); 
    }catch (e){ 
     // Internet Explorer Browsers 
     try{ 
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try{ 
       ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e){ 
       // Something went wrong 
       alert("Your browser broke!"); 
       return false; 
      } 
     } 
    } 
    var strURL = "find_ccode.php?country="+thisval; 
    ajaxRequest.onreadystatechange =getresult;  
    ajaxRequest.open("GET", strURL, true); 
    ajaxRequest.send(null); 
} 
function getresult(){ 
    if(ajaxRequest.readyState == 4){ 
     document.getElementById('cur_code'+num).value = ajaxRequest.responseText; 
    } 
} 
+0

當我在javascript中警告但ajax部分不起作用時,它會返回值。我應該如何解決在PHP文件? 我目前使用的是 $ country = $ _ REQUEST ['country']; echo $ country; 你有什麼建議? – user1328273 2012-04-24 03:38:11

+0

你應該使用get方法。改變這一行$ country = $ _ GET ['country']; – nithi 2012-04-24 03:47:49

+0

感謝您的回覆,但它不會在每行發送文本字段時顯示任何內容。 – user1328273 2012-04-24 03:54:03

相關問題