[英]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
. 目前,我在foreach
循环中有多个具有相同id的文本框和多个下拉列表。我试图在更改为相关textbox
获取dropdown
选定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 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: 您可以通过三种方式解决此问题:
.class
instead of an #id
. 使用.class
而不是#id
。 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()) }); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.