在选择读取选项值 [英] Reading option values in select

查看:150
本文介绍了在选择读取选项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有选择值如下:

 <选择ID =选择框多个=多​​>            <%的foreach(在Model.Name VAR项)
            {%GT;
                <期权价值=<%= item.Value%GT;><%= item.Text%GT;< /选项>
            <%}
            %GT;            < /选择>

我在jQuery的一个功能,将要读文本和值。我需要在数组中的值,这样我可以在表一中列ID和另一列文本显示。我的问题是,我中号不能单独检索每个值。我中有一行文字男,test1test2test3。

 函数read(){
            $(#选择框)。每个(函数(){
                VAR值= $(本).VAL();
                变种文字= $(本)的.text();
                警报(值);警报(文本);            });
        }


解决方案

您正在接近。您需要遍历<选项> S,而不是<选择> 元素:

  $(#选择框选项)。每个(函数(){
  VAR值= $(本).VAL();
  变种文字= $(本)的.text();
  警报(值);
  警报(文本);
}

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();
                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);
}

这篇关于在选择读取选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆