使用第二个jquery和php填充文本字段

时间:2016-12-26 11:53:34

标签: php jquery mysql

我需要以下代码的帮助。我试图用行集中的数据填充文本字段,但我想在结果中使用另一个字段。所以下拉列表给了我公司名称,我想用街道和国家等填充文本字段。

提前致谢

<form>
<select name="name" id="name" onchange="">
<?php
do {  
 ?>
  <option value="<?php echo $row_Recordset1['CustomerCompanyName']?>"<?php if (!(strcmp($row_Recordset1['CustomerCompanyName'],     ucwords($row_Recordset1['CustomerCompanyName'])))) {echo     "selected=\"selected\"";} ?>>
  <?php echo $row_Recordset1['CustomerCompanyName']." | ".$row_Recordset1['Street'] ?></option>
  <?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
  $rows = mysql_num_rows($Recordset1);
  if($rows > 0) {
      mysql_data_seek($Recordset1, 0);
      $row_Recordset1 = mysql_fetch_assoc($Recordset1);

  }

?>

</select>

<input name="firstname" id="firstname" type="text" />

<script type="application/javascript">

$(document).ready(function(){
    $('#name').on('change', function () {
        var selection = $(this).val();
    $('#firstname').val(selection);
    });
});

</script>

1 个答案:

答案 0 :(得分:1)

考虑到您将公司数据存储在选项文本中,如下所示:

<option value="companyName">companyName|street|country</option>

然后您应该获取所选的选项文本,将其拆分并使用碎片数据填充您需要的字段。

<form>
    <select name="name" id="name" onchange="">
            <option value="google">google|18st.|usa</option>
            <option value="facebook">facebook|25st.|usa</option>
            <option value="microsoft">microsoft|05st.|usa</option>
    </select>

    <input name="firstname" id="firstname" type="text" />
    <input name="street" id="street" type="text" />
    <input name="country" id="country" type="text" />
</form>
<script type="application/javascript">
    $(document).ready(function(){
        $('#name').on('change', function () {
            var companyData =  $(this).find("option:selected").text().split("|");

            var companyName = companyData[0];
            var street = companyData[1];
            var country = companyData[2];

            $('#firstname').val(companyName);
            $('#street').val(street);
            $('#country').val(country);
        });
    });
</script>