当我在下拉列表中选择错误时发出警报消息

时间:2019-09-17 15:37:00

标签: javascript c# asp.net

我有两个下拉列表,一个让我们假设ddl1和ddl2。如果我选择ddl2值(如果我选择了某个值,假设它与ddl1值不相关),则两个下拉列表都绑定在aspx页中的硬编码我想要的

我的代码

public void BindSub()
        {
            Cmd = new SqlCommand("SP_P_BindSub", Con.OpenConnection());
            Cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(Cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            ddlSBU.DataSource = dt;
            ddlSBU.DataValueField = "ID";
            ddlSBU.DataTextField = "SBU_Name";
            ddlSBU.DataBind();

            ddlSBU.Items.Insert(0, new ListItem("Select", "0"));
        }

        protected void ddlSBU_SelectedIndexChanged(object sender, EventArgs e)
        {
            Cmd = new SqlCommand("SP_P_BindDepartment", Con.OpenConnection());
            Cmd.CommandType = CommandType.StoredProcedure;
            Cmd.Parameters.AddWithValue("@SBU_ID", ddlSBU.SelectedValue);
            SqlDataAdapter da = new SqlDataAdapter(Cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            ddlDepartment.DataSource = dt;
            ddlDepartment.DataValueField = "ID";
            ddlDepartment.DataTextField = "Department_Name";
            ddlDepartment.DataBind();
            ddlDepartment.Items.Insert(0, new ListItem("Select", "0"));
        }





<script >
       $(document).ready(function () {
           $("#ddlemployeetype").change(function () {
               value1 = document.getelementbytext("ddlemployeetype").value;
               value2 = document.getelementbytext("ddlrole").value;


              case "Employee":
                   document.getElementById("subcategory").options[0] = new Option("select", "");
                   document.getElementById("subcategory").options[1] = new Option("Cordinator", "Cordinator");
                   document.getElementById("subcategory").options[2] = new Option("Filecer", "Filecer");

                   break;

               case "Contractor":
                   document.getElementById("subcategory").options[0] = new Option("select", "");
                   document.getElementById("subcategory").options[1] = new Option("Leader", "Leader");
                   document.getElementById("subcategory").options[2] = new Option("Member", "Member");
                   break;
                   break;

           return true;

               if (value1 != value2) {
                   alert("not related value");
               }

           });
       });



</script>

我的ddl1值是Contractor和employee,而ddl2值是cordinator,leader,facilator,leader2,member。 条件:如果我选择了其他东西,则调解员和咨询员属于雇员,然后向我显示错误选择的消息

1 个答案:

答案 0 :(得分:0)

您可以使用jQuery来做到这一点:

$(document).ready(function () {
    $("#ddl1").change(function () {
        value1 = document.getElementById("ddl1").value;
        value2 = document.getElementById("ddl2").value;

        // here write your business logic
        if (value1 != value2) {
            alert("not related value");
        }

    });
});