从下拉框中获取选定的值,并使用填充文本框php javascript

时间:2015-02-21 10:30:14

标签: javascript php jquery

我根据列表框已更改

在文本框中填充值

我的代码正在运行......并且还能正确获取数据......

但这是基于id。

我需要根据城市名称选择数据。

下面是我的代码

<?php
require_once('includes/config.php');
?>

<?php
$result1 = $database->getRows("SELECT * from areamaster");
?>
<html>
<head>

<script type="text/javascript">
    var compInfoArray = [
        <?php                       
           foreach($result1 as $row1){ 

                echo 'id : ' . $row1['id'] . ',';
                echo 'cityname :  "'.$row1['cityname'].'",';
                echo 'area : "'.$row1['area'].'"';
            }
        ?>
    ];

    function showname() {
  var id = document.form1.id.value;
  var index = compInfoArray.contains(id);
  document.form1.cityname.value = compInfoArray[index]["cityname"];
  document.form1.area.value = compInfoArray[index]["area"];
}

window.onload = function() {
  showname();
}


Array.prototype.contains = function(needle) {
  for (var i in this) {
    for (var key in this[i]) {
      if (this[i][key] == needle) {
        return i;
      }
    }
  }
  return false;
}
</script>

  </head>
</head>
<body>
<form name="form1">
  <select name="id" onChange="showname()">
<?php foreach($result1 as $row1){   ?>  
    <option  value="<?php echo $row1["cityname"]; ?>"><?php echo $row1["cityname"]; ?></option>
    <?php }?>       
  </select>
  <label>
    <input type="text" name="cityname" value="" />
    <input type="text" name="area" value="" />
  </label>

</form>
</body>
    </html>

1 个答案:

答案 0 :(得分:1)

我尝试用简约的.html重现你的案例:

<form name="form1">  

    <select name="id" onChange="showname()">

       <option  value="Paris">Paris</option>
       <option  value="London">London</option>
       <option  value="Berlin">Berlin</option>          

    </select>
    <label>
        <input type="text" name="cityname" value="" />
        <input type="text" name="area" value="" />
    </label>

</form>

所以,如果我理解正确,这就是你得到的HTML。

接下来,我以不同的方式重建了你的js数组:

var compInfoArray = [
    {
        "id" : 5,
        "cityname" : "Paris",
        "area" : "FR"
    },
    {
        "id" : 8,
        "cityname" : "London",
        "area" : "UK"
    },
    {
        "id" : 12,
        "cityname" : "Berlin",
        "area" : "GER"
    }
];

然后我写了一个原型,它会在任何子数组键中找到一个值:

Array.prototype.contains = function (needle) 
{
   for (var i in this) 
   {
       for(var key in this[i]) 
       {
            if(this[i][key] == needle )
            {
                return i;
            }
        }
   }
   return false;
}

最后你的showsame()函数:

function showname() 
{
    var id = document.form1.id.value;
    var index = compInfoArray.contains(id);
    document.form1.cityname.value = compInfoArray[index]["cityname"];
    document.form1.area.value = compInfoArray[index]["area"];
}

工作片段:

&#13;
&#13;
var compInfoArray = [{
  "id": 5,
  "cityname": "Paris",
  "area": "FR"
}, {
  "id": 8,
  "cityname": "London",
  "area": "UK"
}, {
  "id": 12,
  "cityname": "Berlin",
  "area": "GER"
}];



function showname() {
  var id = document.form1.id.value;
  var index = compInfoArray.contains(id);
  document.form1.cityname.value = compInfoArray[index]["cityname"];
  document.form1.area.value = compInfoArray[index]["area"];
}

window.onload = function() {
  showname();
}


Array.prototype.contains = function(needle) {
  for (var i in this) {
    for (var key in this[i]) {
      if (this[i][key] == needle) {
        return i;
      }
    }
  }
  return false;
}
&#13;
<form name="form1">
  <select name="id" onChange="showname()">

    <option value="Paris">Paris</option>
    <option value="London">London</option>
    <option value="Berlin">Berlin</option>

  </select>
  <label>
    <input type="text" name="cityname" value="" />
    <input type="text" name="area" value="" />
  </label>

</form>
&#13;
&#13;
&#13;

编辑:您可能需要使用查询结果构建数组:

var compInfoArray = [

        <?php                       
            foreach($result1 as $row1)
            { 
                echo '{';
                echo 'id : ' . $row1['id'] . ',';
                echo 'cityname :  "'.$row1['cityname'].'",';
                echo 'area : "'.$row1['area'].'"';
                echo '},';
            }
        ?>
    ];