将Java脚本与HTML选择结合使用 [英] Using Java Script with Html select
问题描述
我正在使用html< select>作为下拉列表.从xml文件填充项目.
我使用Java脚本功能来禁用< select>的某些元素.
为此,我使用registerstaterscript来注册函数.
以上所有步骤均能成功完成.
问题是我想验证< select>选定索引更改时的元素(即onchange事件不起作用.)
我在这里发送代码.
Java脚本代码:-
I am using a html <select> as dropdownlist.Filling items from a xml file.
I use Java script function to disable some element of <select>.
for this i use registerstaterscript to register function.
All the above steps work succesfully.
The problem is that i want to validate the <select> element when selected index changed(i.e. onchange event does not work.)
I am sending here the code.
Java Script Code:--
<script type="text/javascript">
function ErrCurLoc(id) {
var ddl = document.getElementById('id');
var x = document.getElementById('id').selectedIndex;
var selItem = ddl.options[x].text;
if (selItem == "Select") {
document.getElementById("spErrCurLoc").style.visibility = "visible";
}
}
</script>
在aspx页面中选择html的代码:-
Code for html select in aspx page:--
<select id="ddlCurLoc" runat="server" önchange="ErrCurLoc(this.ClientId);">ct>
我也无法在许多地方将Java脚本代码用于服务器或html控制.
但是我无法捕获该错误.为什么我不知道.谁能帮助我如何在ASP.Net中使用Java脚本代码.
I am also fail to use java script codes for server or html control at many places.
But i cannot catch the bug.why i do not know.Can any one help me how to use java script code in ASP.Net.
推荐答案
您好,
我尝试了一些符合您要求的代码.只需检查一次
Hi,
I tried some code for your requirement.just check this once
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language ="javascript" >
function f2(tid) {
if (tid.value == "third") {
alert("third");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="drplst" onchange ="f2(this)">
<option value ="frst">sdfsdfd</option>
<option value ="scnd">sdgfhgffsdfd</option>
<option value ="third">sdfhgfhsdfd</option>
</select>
</div>
</form>
</body>
</html>
我希望它也对您有用
最好的
I hope it works for you too
All the Best
通过修改Murali的代码,我解决了我的问题,如下所示:
Hi,By modifying Murali''s code i solve my problem as follows:
function ErrCurLoc(id) {
var x = id.selectedIndex;
var val = id.options[x].value;
if (val == "Select") {
document.getElementById("spErrCurLoc").style.visibility = "visible";
}
else {
document.getElementById("spErrCurLoc").style.visibility = "hidden";
}
}
<select id="ddlCurLoc" runat="server" önchange="ErrCurLoc(this)"></select>
但是当我使用
But when i use
var val= id.options[x].text;
if (val == "Select")
//code
为什么不起作用?
it does not work why?
这篇关于将Java脚本与HTML选择结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!