开发者

Reading option values in select

I have select values as follows:

<select id="SelectBox" multiple="multiple">

            <% foreach (var item in Model.Name)
            { %>
                <option value="<%= item.Value %>"><%=item.Text%></option>
            <% } 
            %>

            </select>

I have a function in jquery that will have to read both the text and value. I need to have the values in array so that I can display them in a table with a column Id and another column text. My problem is that Im not able to retrieve each and every value separately. Im having the text in one line, test1test2test3.

function read() {
            $("#SelectBox").each(function() {
                var value = $(this).val();
        开发者_如何学JAVA        var text = $(this).text();
                alert(value);alert(text);

            });
        }


You're close. You need to iterate over the <option>s, not the <select> elements:

$("#SelectBox option").each(function() {
  var value = $(this).val();
  var text = $(this).text();
  alert(value);
  alert(text);
}


Try

function read() {
            $("#SelectBox > option").each(function() {
                var value = $(this).val();
                var text = $(this).text();
                alert(value);alert(text);

            });
        }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜