开发者

Reading option values in select

开发者 https://www.devze.com 2022-12-24 10:30 出处:网络
I have select values as follows: <select id=\"SelectBox\" multiple=\"multiple\"> <% foreach (var item in Model.Name)

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

精彩评论

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

关注公众号