简体   繁体   中英

Insert Drop down Value to a multiple text boxes

I have a table inside a form

<table id="tablereport2" 
class="table table-striped table-bordered table-hover table-condensed">
            <thead style="background-color:black; font-weight:bold; 
        color:aliceblue">
                <tr>

            </thead>
            <tr>
                <th>Class ID</th>
                <th>Course Title</th>
                <th>Department</th>
                <th>SID</th>
                <th>Full Name</th>
                <th>Minutes</th>
            </tr>

            @for (var i = 0; i < Model.Count; i++)
            {
                <tr id="texbox">
                    <td>
                        @Html.DisplayFor(model => model[i].ClassID)
                        @Html.HiddenFor(model => model[i].ClassID)
                    </td>
                    <td>
                        @Html.DisplayFor(model => model[i].CourseTitle)
                        @Html.HiddenFor(model => model[i].CourseTitle)
                    </td>
                    <td>
                        @Html.DisplayFor(model => model[i].Department)
                        @Html.HiddenFor(model => model[i].Department)
                    </td>
                    <td>
                        @Html.DisplayFor(model => model[i].SID)
                        @Html.HiddenFor(model => model[i].SID)
                    </td>
                    <td>
                        @Html.DisplayFor(model => model[i].FullName)
                        @Html.HiddenFor(model => model[i].FullName)
                    </td>
                    <td>
                      @Html.TextBoxFor(model => model[i].Minutes, new { 
         placeholder = "0", id = "minutes" })
                        @Html.ValidationMessageFor(model => 
         model[i].Minutes)
                    </td>
                </tr>
            }
        </table>
        <table align="center">
            <tr>
                <td>
                    <input type="hidden" value="Save" id="btn-submit" 
                 class="btn btn-default" />
                    <span>
                        @Html.ActionLink("Save", "Class", "Class",
                      new { @class = "btn btn-success btn-md insertBtn", 
                 StartTime = Dat

I want to populate the textbox using the drop-down menu that I have in the upper right corner.

Rite now works but just populate the first one doesn't go through the loop.

<td align="right">
<b>@Html.Label("Select Minutes :")</b>
    <select id="dropdown">
        <option value="50">50</option>
        <option value="100">100</option>
    </select>
</td>

My Java Script

<script>
$(function () {
    $('#dropdown').change(function () {
        var minutes = $(this).val();
        $('#minutes').val(minutes);
    });
});

Any Idea guys I would like to do this the simplest way possible.

Thanks for the help

You are using the same minute id for your elements so it will just stop after the first match since id elements must be unique. Add a minutes class instead and you are set.

 $(function() { $('#dropdown').change(function() { var minutes = $(this).val(); if (minutes != "") $('.minutes').val(minutes); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="minutes" /><br/> <input class="minutes" /><br/> <input class="minutes" /><br/> <input class="minutes" /><br/> <select id="dropdown"> <option value="">Select</option> <option value="50">50</option> <option value="100">100</option> </select>

As already pointed out in the comments ids should be unique. So using a class instead of an id is what you need.

Change

id = "minutes”

To

class = "minutes”

And

$('#minutes').val(minutes);

To

$('.minutes').val(minutes);

That's it!

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