如何使用 Javascript 从 asp.net 中的下拉列表中获取选定的值?

发布于 2024-08-24 14:34:21 字数 445 浏览 5 评论 0 原文

我正在从数据库填充国家/地区下拉列表。我需要从下拉列表中选择一个值并使用 Javascript 将其分配给文本框。

代码:

var textboxId = document.getElementById("txtCountry");
var dropdownListId =document.getElementById("ddlLocation"); 

var e = document.getElementById("ddlLocation"); 
var strUser = e.options[e.selectedIndex].value;

document.getElementById(textboxId).value = strUser;    
document.getElementById(textboxId).focus(); 

这样做我收到错误。有什么解决办法吗?

I am populating country dropdownlist from a database. I need to select a value from dropdown list and assign it to textbox by using Javascript.

Code:

var textboxId = document.getElementById("txtCountry");
var dropdownListId =document.getElementById("ddlLocation"); 

var e = document.getElementById("ddlLocation"); 
var strUser = e.options[e.selectedIndex].value;

document.getElementById(textboxId).value = strUser;    
document.getElementById(textboxId).focus(); 

by doing this I am getting error. Any solutions?

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

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

发布评论

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

评论(4

小清晰的声音 2024-08-31 14:34:21

你的代码是错误的,看看我对相同代码进行了更改:

var textboxId = document.getElementById("txtCountry");
var e = document.getElementById("ddlLocation"); 
var strUser = e.options[e.selectedIndex].value;
textboxId.value = strUser;    
textboxId.focus(); 

你做了什么,你选择了文本框,JS 返回了该文本框的 DOM 元素,你想通过传递 getElementById() 函数内文本框的 >DOM。

这里是它的问题所在:

document.getElementById(textboxId).value = strUser;

要使用 getElementById() 方法,您需要传递元素 id 的字符串值。

希望这有帮助。

Your code is wrong, Look at where I've made the changes to the same code:

var textboxId = document.getElementById("txtCountry");
var e = document.getElementById("ddlLocation"); 
var strUser = e.options[e.selectedIndex].value;
textboxId.value = strUser;    
textboxId.focus(); 

What you did, is you selected your textbox and JS returned you a DOM element of that text box and you wanted to populate it by passing the DOM of the textBox inside the getElementById() function.

Here is where it broke:

document.getElementById(textboxId).value = strUser;

To use getElementById() method, you pass a string value of the id of an element.

Hope this helps.

满天都是小星星 2024-08-31 14:34:21

尝试使用:

document.getElementById('<%=txtCountry.ClientID%>').value

var textBox = document.getElementById('<%=txtCountry.ClientID%>');
textBox.value = strUser;

那是因为生成的文档中的 html 元素的 id 与您在代码中分配的 id 不匹配。要在 html 中获取分配给控件的 ID,您可以使用下拉列表的 ClientID 属性。

另一个问题是您将 html 元素分配给变量,然后使用 getElementById 函数,这是无效的调用。

这在即将发布的 ASP.NET 4 中发生了变化。

希望有帮助!

Try with:

document.getElementById('<%=txtCountry.ClientID%>').value

or

var textBox = document.getElementById('<%=txtCountry.ClientID%>');
textBox.value = strUser;

That's because the ids of the html elements in the generated documents doesn't match with the id that you have assigned in your code. To get the id assigned to your control in the html, you can use the ClientID property of your dropdown.

Another problem is that you assign yourhtml element to variable and then use getElementById function which is not valid call.

This is changed in ASP.NET 4, that is about to be released.

Hope that helps!

逆蝶 2024-08-31 14:34:21

这两行:

document.getElementById(textboxId).value = strUser;    
document.getElementById(textboxId).focus(); 

也是错误的。如果您的上一行确实有效:

var textboxId = document.getElementById("txtCountry");

那么您所调用的 textboxId 实际上将是文本框控件,因此您将使用该控件而不是字符串标识符来执行 getElementById

遵循@anthares所说的;试试这个:

var textboxId = '<%=txtCountry.ClientID%>';
alert('My textbox id is: '  + textboxId);

并确保您获得了文本框的正确 ID(请记住,它将被 ASP.Net 修改,至少确保您没有得到任何结果)。然后,当您执行 document.getElementById 时,您需要在使用之前检查结果:

var myTextBox = document.getElementById(textboxId);
if (myTextBox !== null) {
    ...now i can access the properties...
}

These two lines:

document.getElementById(textboxId).value = strUser;    
document.getElementById(textboxId).focus(); 

are wrong too. If your previous line actually worked:

var textboxId = document.getElementById("txtCountry");

then what you have called textboxId will actually be the textbox control, so you will be doing a getElementById using the control instead of a string identifier.

To follow upon what @anthares said; try this:

var textboxId = '<%=txtCountry.ClientID%>';
alert('My textbox id is: '  + textboxId);

and make sure that you are getting the correct ID for the textbox (remember that it will be munged by ASP.Net, at least make sure you are not getting nothing). Then when you do a document.getElementById you need to check the result before using:

var myTextBox = document.getElementById(textboxId);
if (myTextBox !== null) {
    ...now i can access the properties...
}
酒与心事 2024-08-31 14:34:21

如果您不想使用,

    document.getElementById()

请尝试:

    var VarName = $('#<%=YouDropDownId.ClientId %>').val();

If you don't want to use

    document.getElementById()

try it:

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