简体   繁体   English

根据下拉选择在文本框中显示值

[英]Show value in textbox based on dropdown selection

I'm trying to show values in the textbox based on select values.我正在尝试根据 select 值在文本框中显示值。 For example, If I select a customer name from the select element, then his detail like address, the phone number needs to be dynamically filled in those textboxes例如,如果我 select 元素来自 select 元素,那么他的详细信息就像地址,电话号码需要在那些文本框中动态填写

Here is MySQL table这是 MySQL 表

Table Name: Customer表名:客户

Table Image表格图像

MYSQL and PHP MYSQL 和 PHP

<?php
$select_customer = "select * from customer";
$select_customer_query = mysqli_query($connection, $select_customer); // Customer Data
?>

      <form method="post" action="">
      <div class="form-group">
      <select class="form-control" name="customer_name">
      <option>Customer Name</option>
      <?php while($customer_result = mysqli_fetch_assoc($select_customer_query)) { ?>
      <option><?php echo $customer_result['name']; ?></option>
      <?php } ?>
      </select>
      </div>
      <div class="form-group">
      <input type="text" class="form-control" placeholder="Phone Number">
      </div>
      <div class="form-group">
      <input type="text" class="form-control" placeholder="Website">
      </div>
      </form>

You need to use onChange to get the value when selected value changed您需要使用onChange来获取所选值更改时的值

For getting values like phone number and website you can add other values as attribute to option and get then on change要获取电话号码和网站等值,您可以将其他值作为属性添加到选项,然后进行更改

Now when onChange Called you need to get that attribute values from selected option and set them in text boxes using jQuery现在调用onChange时,您需要从选定选项中获取该属性值并使用 jQuery 在文本框中设置它们

  <form method="post" action="">
      <div class="form-group">
      <select class="form-control" name="customer_name">
      <option>Customer Name</option>
      <?php while($customer_result = mysqli_fetch_assoc($select_customer_query)) { ?>
      <option website="<?php echo $customer_result['website']; ?>" phone_number="<?php echo $customer_result['phone_number']; ?>" ><?php echo $customer_result['name']; ?></option>
      <?php } ?>
      </select>
      </div>
      <div class="form-group">
      <input id="phone_number" type="text" class="form-control" placeholder="Phone Number">
      </div>
      <div class="form-group">
      <input id="website" type="text" class="form-control" placeholder="Website">
      </div>
      </form>
<script>
$(document).ready(function () {     
$('select[name="customer_name"]').change(function(){
   var website = $('option:selected', this).attr('website');
   $("#website").val(website);
   var phone_number = $('option:selected', this).attr('phone_number');
   $("#phone_number").val(phone_number);
});
});
</script>

$('select[name="customer_name"]').change(function(){})

here it will detect the change by selecting that element using its name在这里它将通过使用其名称选择该元素来检测更改

`

$('option:selected', this).attr('website'); $('option:selected', this).attr('website');

` this will get attribute by name we pass in it like currently website from selected option ` 这将通过名称获取属性,就像从选定选项中传入的当前网站一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM