简体   繁体   English

JQuery/Javascript - 如何<label>根据其对应的</label>变化<select>

[英]JQuery/Javascript - How to change <label> based on its corresponding <select>

I want to change a label if its corresponding dropdown has changed value (In the same row).如果相应的下拉列表更改了值(在同一行中),我想更改标签。 this is the html page.这是 html 页面。

在此处输入图片说明

Each dropdown and label has different ids.每个下拉菜单和标签都有不同的 ID。 For example if there is 4 dropdowns then their ids will be dropdown1, dropdown2, dropdown 3, dropdown 4. same with label.例如,如果有 4 个下拉列表,那么它们的 id 将是 dropdown1、dropdown2、dropdown 3、dropdown 4。与标签相同。 But my problem is I don't know how to correspond or chain the two elements together, for example: dropdown 1 should be connected to label 1, meaning whenever the select1 dropdown has changed the label1 will change (by displaying the dropdown1's current selected option).但我的问题是我不知道如何将两个元素对应或链接在一起,例如:下拉列表 1 应该连接到标签 1,这意味着每当 select1 下拉列表发生更改时,标签 1 都会更改(通过显示下拉列表 1 的当前选定选项)。

Here's my snippet source code for HTML:这是我的 HTML 片段源代码:

<td> 
    <select class="form-control" id = "bookingTypeDropDown{{$counter1}}"> <!-- Assigning of unique ids based on iteration number -->
           @foreach($booking_types as $booking_type)
                <option value = "{{$booking_type->book_type_price}}">
                     {{$booking_type->book_type_name}}
                </option>
           @endforeach
    </select>
</td>
@foreach($depart_destination_country as $destination)
    <td>
        <input type="radio" 
               name="departure_flight_id" 
               value="{{$flight->flight_id}}" required>
         <label id = "calculatedFare{{$counter1}}">price here</label> <!-- Assigning of unique ids based on iteration number -->

         <input type="hidden" name="total_departure_fare" value="#" required>   
    </td>
@endforeach

This picture below is the html for the snippet source code provided above:下图是上面提供的片段源代码的 html:

在此处输入图片说明

Source code (Responsible for the changing of label when select was changed):源代码(负责更改select时标签的更改):

<script type = "application/javascript">

window.onload = function() {

    var numItems = $('.form-control').length; //Counts the number of <select> by class
    var dropdowns =[];
    var labels = [];

    for(var i=0; i<numItems; i++) //Iteration for getting all <select> and <labels> and storing them in their respective arrays
    {
        dropdowns[i] = document.getElementById("bookingTypeDropDown" + i);
        labels[i] = document.getElementById("calculatedFare" + i);
    }

    var currentElement; 

    for(var d=0; d<dropdowns.length; d++)
    {
        var price;

        $(dropdowns[d]).change(function (){ //if dropdown object changed option
            var price = $(this).val(); //getting of option's value
            currentElement = d; //getting of array index where change was detected
            console.log(price);             //printing only
            console.log(currentElement);    //printing of what index does the changed occured
        });

        $(labels[currentElement]).text("PHP " + price); //
    }

}

The problem in my javascript code is that it always print the last index or iteration number (which is 4 in this scenario) but the values being printed are right.我的 javascript 代码中的问题是它总是打印最后一个索引或迭代号(在这种情况下为 4),但打印的值是正确的。 for example if I clicked the first dropdown it prints the right value but not the right index (prints index 4 instead of index 1).例如,如果我单击第一个下拉列表,它会打印正确的值而不是正确的索引(打印索引 4 而不是索引 1)。 No problem on value.价值没有问题。

What I want is the right detection of element that has been changed option and once it has been detected it should change its corresponding label.我想要的是正确检测已更改选项的元素,一旦检测到它就应该更改其相应的标签。

Scenario example:场景示例:

Select1 = Label1选择 1 = 标签 1
Select2 = Label2选择 2 = 标签 2
Select3 = Label3选择 3 = 标签 3
.... ....

How do i coordiante a select to a label?我如何将选择协调到标签?

You can simply do it by using selectors and classes, you don't have to setup and enumerate through nested IDs.您可以简单地通过使用选择器和类来完成,您不必通过嵌套 ID 进行设置和枚举。

Please run the below code snippet.请运行以下代码片段。

 $(() => { $('.select-price').on('change', function() { onChange(this); }); function onChange(selectEl) { const $row = $(selectEl).parents('tr'); const $label = $('td:eq(1) label', $row); const $departure_flight_input = $('td:eq(1) input[name="departure_flight_id"]', $row); $label.text($(selectEl).val()); $departure_flight_input.val($(selectEl).val()); } function getInitialPrice() { $('.select-price').each((index, el) => onChange(el)); } getInitialPrice(); });
 <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script> <table> <tr> <td> <select class="form-control select-price"> <option value="1.00">1.00</option> <option value="2.00">2.00</option> <option value="3.00">3.00</option> </select> </td> <td> <input type="radio" name="departure_flight_id" value="" required> <label>price here</label> </td> <tr> <tr> <td> <select class="form-control select-price"> <option value="1.10">1.10</option> <option value="2.20">2.20</option> <option value="3.30">3.30</option> </select> </td> <td> <input type="radio" name="departure_flight_id" value="" required> <label>price here</label> </td> <tr> </table>

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

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