php html form选择填充文本字段jquery的选项

时间:2016-01-25 00:58:44

标签: javascript php jquery html

我正在编辑wordpress插件:

它目前有一个用户的下拉选项列表,并且根据该用户,我想用该用户的电子邮件填写文本字段。

选择选项下拉列表:

<div>
    <label for="Customer_ID"></label>
    <select name="Customer_ID" id="Customer_ID" />
    <option value='0'>None</option>
    <?php foreach ($Customers as $Customer) { ?>
      <option value='<?php echo $Customer->Customer_ID; ?>'><?php echo $Customer->Customer_Name; ?></option>
    <?php } ?>
    </select>
</div>

客户电子邮件存储在$ Customer-&gt; Customer_Email

我试图在用户更改所选用户时填写以下值:

<div class="form-field">
  <label for="Order_Email"></label>
  <input type='text' name="Order_Email" id="Order_Email" />
</div>

我已经玩过使用以下脚本填充Order_Email,但它只是将值作为customer_id。如何更改电子邮件的值

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $('#Customer_ID').on('change', function () {
        var selection = $(this).val();
        $('#Order_Email').val(selection);
    });
});
</script>

1 个答案:

答案 0 :(得分:0)

您可以在选项中添加title属性,使其等于电子邮件值:

在此处使用它:http://jsfiddle.net/7BUmG/3876/

这可能不是最好的解决方案和hacky,但它应该可以解决问题。插件更新后,这也会中断

<div>
    <label for="Customer_ID"></label>
    <select name="Customer_ID" id="Customer_ID" />
    <option value='0'>None</option>
    <?php foreach ($Customers as $Customer) { ?>
      <option title='<?php echo $Customer->Customer_Email; ?>'value='<?php echo $Customer->Customer_ID; ?>'><?php echo $Customer->Customer_Name; ?></option>
    <?php } ?>
    </select>
</div>

$(document).ready(function(){
    $('#Customer_ID').change(function() {
    var email = $(this).find("option:selected").attr("title");
    $('#Order_Email').val(email);
});
});