如何通过javascript从ajax.net ComboBox获取选定的值

发布于 2024-09-03 17:30:11 字数 398 浏览 3 评论 0原文

我需要通过 javascript 从 ajax.net 组合框获取选定的值,以便我可以进行一些客户端验证。

最好的方法是什么? 谢谢,


我已经能够通过以下方式获取值:

var combo = $get('ddlComarcas');
var comboHidden = $get('ddlComarcas_HiddenField');
var o4 = combo.getElementsByTagName('li')[comboHidden.value].childNodes[0];

alert('"' + o4.data + '"');

但我仍然需要从 o4.data 中修剪该值。任何人都可以指出如何做那个 Visual Studio 2008 jquery?

I need to get the selected value from an ajax.net combobox throught javascript so that I can do some client side validation.

What's the best way to do this?
Thanks,


I've been able to get the value with this:

var combo = $get('ddlComarcas');
var comboHidden = $get('ddlComarcas_HiddenField');
var o4 = combo.getElementsByTagName('li')[comboHidden.value].childNodes[0];

alert('"' + o4.data + '"');

But I still need to trim the value from o4.data. Anyone can point how to do that visual studio 2008 jquery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似最初 2024-09-10 17:30:11

您可以使用 jQuery 或仅使用 DOM:

jQuery:

var selection = $('#selectID').val();

DOM:

var selection = document.getElementById("selectID").value;

asp.net ->服务器端

JavaScript ->客户端

You can use jQuery or just use DOM:

jQuery:

var selection = $('#selectID').val();

DOM:

var selection = document.getElementById("selectID").value;

asp.net -> server side

javascript -> client side

浅沫记忆 2024-09-10 17:30:11

我认为答案是客户端不存在值,因此无法检索它。有更简单的方法来获取索引(假设初始化已完成)。

selected index:         $find("<%=cboName.ClientID%>").get_hiddenFieldControl().value;
selected index (again): $find("<%=cboName.ClientID%>").get_selectedIndex();
selected text:          $find("<%=cboName.ClientID%>").get_textBoxControl().value;

据我所知,在客户端验证组合框需要对索引或文本有一定的信心,或者需要某种服务器端解决方法。

为了提供主题行的直接答案,可以使用每个组合框值在服务器端创建一个 JavaScript 数组,然后通过选定的索引引用客户端...

代码隐藏:

 // write combobox values to asp:literal
 foreach (ListItem i in cboName.Items)
         litCboValues.Text += "\"" + i.Value.Replace("\"", "\\\"") + "\", ";
 litCboValues.Text = litCboValues.Text.TrimEnd(new char[] {',', ' '});

aspx:

<script>
// array of values
 var cboValues = [ <asp:Literal id="litCboValues" runat="server" /> ];

// add an alert to the combobox to test
function pageLoad()
{
  $find("<%=cboName.ClientID%>").get_textBoxControl().onblur = function () { 
    alert( cboValues[$find("<%=cboName.ClientID%>").get_selectedIndex()] );
  };
}
</script>


<asp:ComboBox id="cboName" runat="server" ...

I think the answer is value doesnt exist client side so it cant be retrieved. There are easier ways to get index tho (assuming whatever initialization is complete).

selected index:         $find("<%=cboName.ClientID%>").get_hiddenFieldControl().value;
selected index (again): $find("<%=cboName.ClientID%>").get_selectedIndex();
selected text:          $find("<%=cboName.ClientID%>").get_textBoxControl().value;

As far as I can tell, validating a combobox on the client requires some faith in index or text, or some kind of server side workaround.

To provide a direct answer to the subject line, a javascript array could be created server side with each combobox value and then referenced client side by selected index...

codebehind:

 // write combobox values to asp:literal
 foreach (ListItem i in cboName.Items)
         litCboValues.Text += "\"" + i.Value.Replace("\"", "\\\"") + "\", ";
 litCboValues.Text = litCboValues.Text.TrimEnd(new char[] {',', ' '});

aspx:

<script>
// array of values
 var cboValues = [ <asp:Literal id="litCboValues" runat="server" /> ];

// add an alert to the combobox to test
function pageLoad()
{
  $find("<%=cboName.ClientID%>").get_textBoxControl().onblur = function () { 
    alert( cboValues[$find("<%=cboName.ClientID%>").get_selectedIndex()] );
  };
}
</script>


<asp:ComboBox id="cboName" runat="server" ...
つ低調成傷 2024-09-10 17:30:11

这在 IE 和 Chrome 中有效(今天) - IE 唯一有用的是调试器 f12 ( - 你可以浏览监视的对象

Following

// i do it on button but you could probably do it on a combo event
Follow

 function addFollowed() {
      var combo = $get('<%= FollowListBox.ClientID %>'); 
      var toFollow = combo.control._textBoxControl.value;

this works (today) in IE and Chrome - about the only thing ie is good for is the debugger f12 (- you can browse through the watched objects

Following

// i do it on button but you could probably do it on a combo event
Follow

 function addFollowed() {
      var combo = $get('<%= FollowListBox.ClientID %>'); 
      var toFollow = combo.control._textBoxControl.value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文