简体   繁体   English

从下拉菜单中选择统计信息后,使用自定义文本自动填充文本字段

[英]Autofill textfield with custom text after selected a stat from dropdown menu

I need some help for this script. 我需要一些有关此脚本的帮助。 It's a script I got from other place and I would like to add a function to it but I really got stuck. 这是我从其他地方获得的脚本,我想向其中添加一个函数,但确实很卡住。 This is the script: 这是脚本:

          <select name="order_status_id">
            <?php foreach ($order_statuses as $order_statuses) { ?>
            <?php if ($order_statuses['order_status_id'] == $order_status_id) { ?>
            <option value="<?php echo $order_statuses['order_status_id']; ?>" selected="selected"><?php echo $order_statuses['name']; ?></option>
            <?php } else { ?>
            <option value="<?php echo $order_statuses['order_status_id']; ?>"><?php echo $order_statuses['name']; ?></option>
            <?php } ?>
            <?php } ?>
          </select>
        </td>
      </tr>
      <tr>
        <td><?php echo $entry_notify; ?></td>
        <td><input name="notify" type="checkbox" value="1" checked="checked" /></td>
      </tr>
      <tr>
        <td><?php echo $entry_comment; ?></td>
        <td><textarea name="comment" cols="40" rows="8" style="width: 99%"></textarea>

I would like to fill the textfield automatically after I selected one name from the dropdown menu. 从下拉菜单中选择一个名称后,我想自动填充文本字段。 for example in the dropdown listbox I selected a status "Processing" then the textfield below will automatically fill with "We are now processing your request. Please wait for X days and we would inform you shortly." 例如,在下拉列表框中,我选择了一个状态“正在处理”,则下面的文本字段将自动填写“我们正在处理您的请求。请等待X天,我们会尽快通知您。”

So when I selected another status "canceled" , the textfield will change it's content with "We have cancelled your request." 因此,当我选择其他状态“已取消”时,文本字段将通过“我们已取消您的请求”来更改其内容。 and so on.. 等等..

Please tell me if my description wasn't clear enough. 如果我的描述不够清楚,请告诉我。 Thank you so much beforehand. 非常感谢您。

You can add a javascript function that will do what you need in the onchange event of your select box like this: 您可以添加一个javascript函数,该函数将在选择框的onchange事件中执行所需的操作,如下所示:

<select id="order_status_id" name="order_status_id" onchange="updateTextBox()">
<textarea id="comment" name="comment" cols="40" rows="8" style="width: 99%"></textarea>

Also I added id values for both elements so we can easily select them with getElementById 我还为两个元素添加了id值,因此我们可以使用getElementById轻松选择它们

This event would fire each time you change the selection. 每次您更改选择都会触发此事件。 You then would implement a simple javascript function updateTextBox() that will update the textbox for you: 然后,您将实现一个简单的javascript函数updateTextBox() ,它将为您更新文本框:

function updateTextBox() {
    var orderStatusSelect = document.getElementById("order_status_id");
    var status = orderStatusSelect.options[orderStatusSelect.selectedIndex].text;
    var myTextBox = document.getElementById("comment");

    if(status == "Processing") {
        //set the value of the text box
        myTextBox.value = "We are now processing your request. Please wait for X days and we would inform you shortly." ;
    } else if(status == "Cancelled") {
        myTextBox.value = "We have cancelled your request!";
    } else if(status == "OtherStatus") {
        myTextBox.value = "another message goes here!";
    }
    //and so on for all different options
}

See this jsFiddle for a running example. 有关运行示例,请参见此jsFiddle

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

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