在特殊情况下禁用javascript中的ddl更改事件
有两个单选按钮和一个下拉列表。如果选中在特殊情况下禁用javascript中的ddl更改事件,javascript,asp.net,Javascript,Asp.net,有两个单选按钮和一个下拉列表。如果选中rad1,并且dropdownlist值发生更改,则执行dropdown change事件。如果选中了rad2,则禁用更改事件(非下拉列表) 瓦尔1 瓦尔2 Val3 Javascript: function ddlClick() { if(document.getElementById('<%=radNew.ClientID%>').checked){ return false; } } 函数ddlClic
rad1
,并且dropdownlist值发生更改,则执行dropdown change事件。如果选中了rad2
,则禁用更改事件(非下拉列表)
瓦尔1
瓦尔2
Val3
Javascript:
function ddlClick() {
if(document.getElementById('<%=radNew.ClientID%>').checked){
return false;
}
}
函数ddlClick(){
if(document.getElementById(“”).checked){
返回false;
}
}
它仍在对
rad2
选择执行dropdownlist的更改事件?检查启用DDLhandler单选按钮的状态,如果选中或未选中。如果选中此项,则在学生DDL的更改后执行任务。如果未选中,则不会执行If中的代码
Bingo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Bingo.aspx.cs" Inherits="RadioButtonChanged.Bingo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
window.onload = function () {
var ddl = document.getElementById("studentDDL");
ddl.onchange = function () {
var radioChecked = document.getElementById("enableDDLHandler");
if (radioChecked.checked) {
document.getElementById("tongo").innerHTML = "Great!!!";
}
};
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Enable : <asp:RadioButton GroupName="Check" ID="enableDDLHandler" runat="server" />
Disable : <asp:RadioButton GroupName="Check" ID="disableDDLHandler" runat="server" />
<asp:DropDownList ID="studentDDL" runat="server">
<asp:ListItem>Bill</asp:ListItem>
<asp:ListItem>Mark</asp:ListItem>
<asp:ListItem>Steve</asp:ListItem>
<asp:ListItem>Linus</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="tongo" runat="server" Text="Nothing" />
</div>
</form>
</body>
</html>
window.onload=函数(){
var ddl=document.getElementById(“studentDDL”);
ddl.onchange=函数(){
var radioChecked=document.getElementById(“enabledlHandler”);
如果(无线电检查。检查){
document.getElementById(“tongo”).innerHTML=“很棒!!!”;
}
};
};
启用:
禁用:
比尔
做记号
史蒂夫
莱纳斯
备选方案1
我们需要覆盖下拉列表的onchange
事件,然后根据条件执行所需的
If the RadioButton is selected then
doYourStuff;
return false;
else
Call __doPostBack function on the Dropdown
示例代码
Default.aspx
<form id="form1" runat="server">
<asp:RadioButton ID="radNew" runat="server" Text="New" GroupName="radSelect" />
<asp:RadioButton ID="radExisting" runat="server" Text="Existing" GroupName="radSelect" />
<asp:DropDownList ID="ddlType" runat="server" AutoPostBack="true" EnableViewState="true"
OnSelectedIndexChanged="ddlType_SelectedIndexChanged">
<asp:ListItem>Val1</asp:ListItem>
<asp:ListItem>Val2</asp:ListItem>
<asp:ListItem>Val3</asp:ListItem>
</asp:DropDownList>
<asp:Label runat="server" id="lbl" Text=""></asp:Label>
</form>
<script type="text/javascript">
document.getElementById('<%=ddlType.ClientID%>').onchange = function () {
if (document.getElementById('<%=radNew.ClientID%>').checked) {
document.getElementById('<%=lbl.ClientID%>').textContent = "From js";
return false;
} else {
setTimeout('__doPostBack(\'ddlType\',\'\')', 0);
}
};
</script>
备选方案2 不要覆盖下拉列表的
onchange
事件,而是将您的条件添加到默认的dropdown onchange
事件中
<asp:DropDownList ID="ddlType" runat="server" AutoPostBack="true" EnableViewState="true"
onchange="if(!ddlCheck()){return false;}" OnSelectedIndexChanged="ddlType_SelectedIndexChanged">
<asp:ListItem>Val1</asp:ListItem>
<asp:ListItem>Val2</asp:ListItem>
<asp:ListItem>Val3</asp:ListItem>
</asp:DropDownList>
<script type="text/javascript">
function ddlCheck() {
if (document.getElementById('<%=radNew.ClientID%>').checked) {
document.getElementById('<%=lbl.ClientID%>').textContent = "From js";
return false;
} else {
return true;
}
}
</script>
瓦尔1
瓦尔2
Val3
函数ddlCheck(){
if(document.getElementById(“”).checked){
document.getElementById(“”).textContent=“From js”;
返回false;
}否则{
返回true;
}
}
加载到浏览器后,上面的下拉列表如下
<select name="ddlType" onchange="if(!ddlCheck()){console.log('to');return false;};
setTimeout('__doPostBack(\'ddlType\',\'\')', 0)" id="ddlType">
因此,首先检查我们的条件,然后根据条件执行其余操作。我已经完成了这项工作。但是我希望它只在客户端完成。@Ami,请参阅更新的答案。检查现在在客户端进行。
<asp:DropDownList ID="ddlType" runat="server" AutoPostBack="true" EnableViewState="true"
onchange="if(!ddlCheck()){return false;}" OnSelectedIndexChanged="ddlType_SelectedIndexChanged">
<asp:ListItem>Val1</asp:ListItem>
<asp:ListItem>Val2</asp:ListItem>
<asp:ListItem>Val3</asp:ListItem>
</asp:DropDownList>
<script type="text/javascript">
function ddlCheck() {
if (document.getElementById('<%=radNew.ClientID%>').checked) {
document.getElementById('<%=lbl.ClientID%>').textContent = "From js";
return false;
} else {
return true;
}
}
</script>
<select name="ddlType" onchange="if(!ddlCheck()){console.log('to');return false;};
setTimeout('__doPostBack(\'ddlType\',\'\')', 0)" id="ddlType">