简体   繁体   中英

how to get dropdown selected option text on the textbox inside foreach loop

currently i am having multiple textboxes and multple dropdowns inside the foreach loop with same id.i have tried to get the dropdown selected text on change to the relevant textbox . currently it works only for the first text box and first dropdown. what can be the reason?

在此处输入图片说明

Here is my code

<div class="child-name-col">
    <div id="header-type" class="foodmenu-dispay-view-header headerbody clearfix">
    @foreach ($showenMenus as $key => $element)
        <div class="daily-col daily-col-200 reset-padding margin-right-20 parent-div">
        <input type="text" id="newmeal"  name="newmeal[]" value="{{ $element->food }}">
        <input type="hidden" id="mealtime"  name="mealtime[]" value="{{ $element->id }}">
        <div class="col-md-12 margin-top-5 dropdown" style="padding-left: 0px; padding-right: 0px;">
             <select id="dropdown"  name="dropdown[]"  class="form-control foodrecipe_item dropdown_item" >
                 <option value="">None</option>
                  @foreach ($meal_list as  $element)
                     <option value="{{ $element->id }}">{{ $element->title }}</option>
                  @endforeach
              </select>

           </div>
        </div>
    @endforeach

 </div>

javascript

$(document).ready(function () {
    $("#dropdown option").filter(function () {
         return $(this).val() == $("#newmeal").val();
    }).attr('selected', true);


    $("#dropdown").on("change", function () {
         $("#newmeal").val($(this).find("option:selected").text())
    });
});

Three ways you can approach this problem:

  1. Use a .class instead of an #id .
  2. Uniquely name each element.
  3. A little of both...

This example is off the top of my head and is intended to point you to a possible solution. It may or may not work out-of-the-box.

 <div class="child-name-col"> <div id="header-type" class="foodmenu-dispay-view-header headerbody clearfix"> @foreach ($showenMenus as $key => $element) <div class="daily-col daily-col-200 reset-padding margin-right-20 parent-div"> <!-- Notice the unique id of these inputs --> <input type="text" id="newmeal-{{$key}}" name="newmeal[]" value="{{ $element->food }}"> <input type="hidden" id="mealtime-{{$key}}" name="mealtime[]" value="{{ $element->id }}"> <div class="col-md-12 margin-top-5 dropdown" style="padding-left: 0px; padding-right: 0px;"> <!-- notice the unique id, and the data attribute --> <select id="dropdown-{{$key}}" data-key="{{$key}}" name="dropdown[]" class="form-control foodrecipe_item dropdown_item" > <option value="">None</option> @foreach ($meal_list as $element) <option value="{{ $element->id }}">{{ $element->title }}</option> @endforeach </select> </div> </div> @endforeach </div> 

 $(document).ready(function () { // this observer will need to be changed. Left as exercise for the reader. $("#dropdown option").filter(function () { return $(this).val() == $("#newmeal").val(); }).attr('selected', true); // using a class to observe. Needs to be something that isn't used elsewhere, though. $(".foodrecipe_item").on("change", function () { var key = $(this).data('key'); $("#newmeal-"+key).val($(this).find("option:selected").text()) }); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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