【问题标题】:How to display Item price in textbox while you have selected item code in combobox? [duplicate]当您在组合框中选择项目代码时,如何在文本框中显示项目价格? [复制]
【发布时间】:2020-04-27 13:37:19
【问题描述】:

我尝试编写代码以在 texbox 中显示值,但该值是基于从工作正常的组合框中选择的值而消除的, 但是我现在想要的是如果我从组合框中选择 ItemCode 我需要 ItemPrice 显示在文本框中 我现在所拥有的是,如果我选择 Item,则 <option>**Item Name**</option> 之间的值将显示在该文本框中,而不是 ItemPrice 以下是我运行良好的示例代码

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" /></script>

<span id="spanSegmentData">

    <?php
    require_once('connection.php');
    $queItem = mysqli_query($connect, "select * from oitm");

    echo "<select name=\"segment_id\" class=\"form-control\" id=\"segment_id\" STYLE=\"width: 300px\" >";
    echo "<option value=\"\" selected=\"selected\">Select a product</option>";

    while ($rowStockIn = mysqli_fetch_array($queItem)) {
        $ItemCode2 = $rowStockIn['ItemCode'];
        $ItemName2 = $rowStockIn['ItemName'];
        $ItemPrice2 = $rowStockIn['ItemPrice'];

        echo "<option value=\"$ItemCode2\">$ItemCode2 $ItemName2</option>";
    }
    echo "</select>";
    ?>

</span>

<span id="dt_title"> <input id="title" name="title" type="text" value="<?php echo "$ItemPrice2";?>" data-title="MB" style="width:300px" readonly> </span>

<script>
    $("#segment_id").change(function () {
        var id = $(this).val(); 
        $("#title").val($("#segment_id option:selected").text());
    });
</script>

Sample of my form 我想允许所有行根据在组合框中选择的值在文本框中显示值 请任何人都可以帮助我在文本框中显示该 ItemPrice

【问题讨论】:

  • 请格式化您的代码,使其更具可读性,并且仅包含代码的相关部分以进行故障排除
  • 你想在 中显示 ItemPrice 吗?

标签: javascript php jquery ajax


【解决方案1】:

1-对于多行使用类而不是 id。在选择框中添加类'segment_id',在文本框中添加类'title',用于在行中显示价格。

2- 在带有数据属性 itemprice 的选项中添加“itemprice”值。

3- 在 jQuery on change 中获取选定的数据属性值并添加到最近行的文本框中。

喜欢。

    <option value=\"$ItemCode2\" data-itemprice = \"$ItemPrice2\">
        $ItemCode2 $ItemName2
    </option>

在jQuery中获取数据属性值并添加到最近行的文本框中。

喜欢

 $(document).ready(function(){
    $(".segment_id").on('change', function () {
        var id = $(this).val(); 
        var itemprice = $('option:selected',this).data('itemprice');
        $(this).closest('tr').find(".title").val(itemprice);
    });
});

DEMO

【讨论】:

  • 非常感谢我的朋友 Shivendra,它运行良好。让我问最后一个问题。实际上我的文件有不止一行,所以这意味着我在我的文件中使用循环和数组,我可以在脚本中更改什么以允许多行自动显示该 itemprice?谢谢
  • 很高兴为您提供帮助。如果您添加有问题的代码,您如何显示表单元素数据是行。会更清楚。
  • 我编辑了您可以检查的代码,我想在文本框中显示值
  • 我更新了ans,请检查
【解决方案2】:
echo "<option value=\"$ItemCode2-$ItemPrice2\">$ItemCode2 $ItemName2</option>";

使用 - 发送 $ItemPrice2 表单值。

在脚本中:

 $("#segment_id").change(function () {
 var _this = $(this).val();
 var res = _this.split("-");
 var id = res[0];
 var price = res[1];

 console.log(price)
 $("#title").val(price);

【讨论】:

  • 如果用户提交表单,将得到 '$ItemCode2-$ItemPrice2' 值而不是 ItemCode2。它会破坏功能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-30
  • 2022-06-10
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
相关资源
最近更新 更多