亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從我的下拉數組中獲取選定的值

如何從我的下拉數組中獲取選定的值

PHP
臨摹微笑 2022-05-27 15:55:00
我在網頁上顯示 3 個產品,每個產品都有自己的下拉選擇選項。我還為每種產品賦予了自己的形式。我擁有的產品選項對于每種產品都是獨一無二的。Product_1 選項{   "frame":[      {         "template_id":"SKU-26x16-SQUARE",         "name":"Square Canvas - Small",         "price":"100",      },      {         "id":3,         "template_id":"SKU-28x28-SQUARE",         "name":"Square Canvas - Medium",         "price":"300",      },      {         "id":3,         "template_id":"SKU-32x32-SQUARE",         "name":"Square Canvas - Large",         "price":"500",      }   ]}Product_2 選項{   "frame":[      {         "id":1,         "template_id":"SKU-16x12-PORTRAIT",         "name":"Small Portrait Mounted",         "price":"100",      },      {         "id":2,         "template_id":"SKU-16x20-PORTRAIT",         "name":"Medium Portrait Mounted",         "price":"300",      },      {         "id":3,         "template_id":"SKU-24x36-PORTRAIT",         "name":"Large Portrait Mounted",         "price":"500",      }   ]}產品_3 個選項{   "frame":[      {         "id":1,         "template_id":"SKU-11x14-PORTRAIT",         "name":"Small Portrait",         "price":"100",      },      {         "id":2,         "template_id":"SKU-16x20-PORTRAIT",         "name":"Medium Portrait",         "price":"300",      },      {         "id":3,         "template_id":"SKU-24x32-PORTRAIT",         "name":"Large Portrait",         "price":"500",      }   ]}現在,問題是無論我從下拉列表中選擇哪個選項,我的表單似乎只發布列表中的最后一個選項。例如,product_1 "name":"Small Portrait"我需要我的表單來發布數組中包含的所有項目,id:1但它只會發布最后一個數組的值id:3。下拉選擇包含每個產品的屬性:<select class="custom-select" id="product-price-{{$i}}-select" name="LeaveType">    <option selected>Choose a frame...</option>    @foreach($attributes->frame as $attribute)        <option            value="{{$attribute->price}}">{{$attribute->name}}</option>    @endforeach</select>以前有沒有人遇到過這個問題并且知道解決方法?我在想我的數組是錯誤的,但希望得到一些建議。那么我的問題是,我的選項數組看起來不錯還是應該進一步嵌套?我是用戶
查看完整描述

1 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

您獲得最后一個選項的原因是因為您在選擇選項時沒有更新它。


首先,我們需要data在選項中添加屬性。


<select class="custom-select" id="product-price-{{$i}}-select" name="LeaveType">

    <option selected>Choose a frame...</option>

    @foreach($attributes->frame as $attribute)

        <option

            value="{{$attribute->price}}"

            data-template-id="{{$attribute->template_id}}"

            data-name="{{$attribute->name}}"

            data-id="{{$attribute->id}}">{{$attribute->name}}</option>

    @endforeach

</select>

現在在您的.custom-select更改事件中。


<script>

    $(document).ready(function () {

        $('.custom-select').on('change', function () {

            $('.custom-select').not(this).val('Choose a frame...');

            var current = $(this).attr('id');

            var res = current.split("-select");

            $('.' + res[0]).html($(this).val());


            // Get the data from selected option

            var selected_option = $(this).find('option:selected');

            var template_id = selected_option.data('template-id');

            var product_name = selected_option.data('name');


            // Now assign it to hidden input field

            $('input[name="product_name"]').val(product_name);

            $('input[name="sku"]').val(template_id);

            $('input[name="price"]').val($(this).val());

        });

    });

</script>

而已。這應該可以解決問題。我沒有測試它。如果有問題讓我知道。


還有一件事。您可以從此表單中刪除值,因為它會在您選擇一個選項時被填充。


查看完整回答
反對 回復 2022-05-27
  • 1 回答
  • 0 關注
  • 123 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號