开发者

Radio Button List Auto Select first in MVC db populated list

开发者 https://www.devze.com 2023-02-20 23:02 出处:网络
I have a table that gets a dynamic source list for each row, with a radio button for each row.It\'s populated at runtime by MVC.How do I automatically select the first one?

I have a table that gets a dynamic source list for each row, with a radio button for each row. It's populated at runtime by MVC. How do I automatically select the first one?

<table class="thisTable">
 开发者_如何转开发    <tr id="thisRow<%: a.Id %>">
        <% foreach (var t in myType) { %>

            <td id="myTypeCheck<%: a.Id %>">
<input type="radio" name="myType" checked id="<%: t.Id %>" onselect="myTypeChange('<%: t.Id %>')" /></td>

<td><%: t.Val %></td>

<% } %>

</table>

That's a basic representation of the table, how would I select the first one of the generated radio buttons? I've tried checked in the input tag, no dice.


Instead of using a foreach loop, use a for loop.

Then if you are rendering the item at index zero, make the radio button's checked attribute checked as follows:

<table class="thisTable">

    <% for (int i = 0; i < myType.Count; i++) { %>
    <tr id="thisRow<%: a.Id %>">
        <td id="myTypeCheck<%: a.Id %>">

            <% if(i == 0) { %>
                <input type="radio" name="myType" 
                    checked="checked" id="<%: t.Id %>" 
                    onselect="myTypeChange('<%: t.Id %>')" />
            <% } else { %>
                <input type="radio" name="myType" 
                    id="<%: t.Id %>" 
                    onselect="myTypeChange('<%: t.Id %>')" />
            <% } %>
        </td>
        <td><%: t.Val %></td>
</tr>
<% } %>
</table>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号