简体   繁体   English

如何使 select 选项从 ci 3 中的数据库中获取并在文本字段中自动填充结果?

[英]How to make select option fetched from database in ci 3 and autofill result in text field?

I want to make select option fetched from database in codeigneter 3 and display result in text field and into span area.我想让 select 选项从 codeigneter 3 中的数据库中获取,并将结果显示在文本字段和 span 区域中。 I made it like this, it becomes one in the views file.我是这样制作的,它成为视图文件中的一个。 However, when selected, all data displayed is only data from row_name.但是,选择后,显示的所有数据都只是来自 row_name 的数据。

<?php $query = $this->db->get('mydb_table'); ?>
<select id="select_id" required>
    <option>Select Account</option>
    <?php foreach ($query->result() as $d) : ?>
        <option value="<?= $d->row_name ?>">Account id. <?= $d->row_id ?></option>
    <?php endforeach ?>
</select>

<div class="input-group mb-3">
    <input id="account_name" type="text" required readonly/>
</div>
<div class="input-group mb-3">
    <input id="account_code" type="text" required readonly/>
</div>
<small>Account category: <span id="account_category"></span></small>

<script>
    $("#select_id").change(function(){
        $("#account_name").val($(this).val());
        $("#account_code").val($(this).val());
        $("#account_category").text($(this).text());
    }); 
</script>

I hope the result is that all ids appear in the selection options, and when one of them is selected in the input field it will display the name and code data according to the selected data., then the text span displays the category.我希望结果是所有id都出现在选择选项中,当在输入字段中选择其中一个时,它将根据所选数据显示名称和代码数据。然后文本跨度显示类别。

Maybe it looks like in this gif image selectoption.gif也许它看起来像这个 gif 图像selectoption.gif

Table Name: mydb_table row = row_id, row_name, row_code, row_category row_id is a primary AUTO_INCREMENT表名:mydb_table row = row_id, row_name, row_code, row_category row_id is a primary AUTO_INCREMENT

in my database looks like:在我的数据库中看起来像:

INSERT INTO `mydb_table` (`row_id`, `row_name`, `row_code`, `row_category`) VALUES
(1, 'john Doe', 'bc34', 'premium'),
(2, 'Emili Doe', 'ac67', 'standard'),
(3, 'Jev Doe', 'abc2', 'premium'),
(4, 'Rachel Doe', '234a', 'standard');

Use data attribute in <option> , later in js you will get this data attribute<option>中使用data属性,稍后在 js 中你会得到这个data属性

<?php foreach ($query->result() as $d) : ?>
    <option value="<?= $d->row_id ?>" data-code="<?= $d->row_code ?>" data-category="<?= $d->row_category ?>" data-name="<?= $d->row_name ?>">Account Name : <?= $d->row_name ?></option>
<?php endforeach; ?>

Then, add listener when <select> is on change然后,当<select>发生变化时添加监听器

<script>
//please always set inside document ready function
$(document).ready(function() {
    $("#select_id").change(function(){
        $("#account_name").val($(this).data('name')); //get data-name attribute and set it to element with id account_name
        $("#account_code").val($(this).data('code')); //get data-code attribute and set it to element with id account_code
        $("#account_category").html($(this).data('category')); //get data-category attribute and set it to element with id account_category
    });
}); 
</script>

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

相关问题 尝试基于选择选项从数据库自动填充输入字段时出现错误 - Getting an error when trying to autofill input field from database based on a select option 如何从从服务器angularjs获取的json对象的select选项字段中显示所选值 - How to display the selected value in select option field from the json object fetched form server angularjs 如何从多行的选择下拉框中制作自动填充输入文本? - How to make autofill input text from a select dropdown box with multiple lines? 从api获取选项时如何在angular js中选择选项 - How to select an option in angular js when options are fetched from an api 如何从 select 菜单中选择 select 文本选项 - How to select text option from select menu 按下键时如何自动填充文本字段 - How to autofill a text field when pressing a key 将文本从表单选择选项传递到文本输入字段 - Pass the text from a form Select-Option to a text input field Vue 2 - 如何选择正确的<select>从 db 获取数据后的选项 - Vue 2 - How to select the correct <select> option after the data is fetched from db 当输入文本字段被填充时,如何使文本区域中的默认值更改/自动填充? - How to make default value in the textarea change/autofill when the input text field is filled? 显示 select 选项中的文本,并在结果中添加元素 - displaying text from select option with added element in the result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM