具有特定值的多个文本字段行使用Ajax

时间:2012-04-23 08:28:43

标签: php javascript html ajax

我有两个文本字段,无论用户在第一个文本字段中输入什么,它都将由Ajax出现在第二个文本字段中。我需要使用for循环来拥有多行,每行都有自己的值。   HTML

<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>

    

的Ajax

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;

请帮帮我。

1 个答案:

答案 0 :(得分: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;
    }
 }
相关问题