找到你要的答案

Q:Show/Hide div by specific text of data bound dropdownlist

Q:显示/隐藏div的数据绑定DropDownList特定文本

I'm new to programming.

I have a databound dropdownlist to a 2013 access db of a list of countries. I want to show/hide a division where there is a different textbox (where you fill your date of immigration) depending on your chosen country.

Example:

If you chose UK as country of birth, the text box for date of immigration will show. But if you chose US as country of birth, the text box for date of immigration will not show.

I thought to use jquery, but it's not working. How do I get the value of the chosen index of the dropdownlist?

Code:

jquery:
$("#birthcountrylist").change(function)
{
if($(this).val()=="US") { $(divimmigratedate).hide(); }
else { $(divimmigratedate).show(); }
}); 

form:
<!-- Entering Birth Country /-->
<asp:DropDownList ID="birthcountrylist" runat="server" AutoPostBack="true" AppendDataBoundItems="true"></asp:DropDownList

<!--Entering Date of Immigration/-->
<div id="divimmigratedate" runat="server" style="display:none">
<asp:TextBox ID="dateimmigrate" runat="server" MaxLength="10" Width="13%" Text="" ></asp:TextBox>

back-code:
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            OleDbConnection db = new OleDbConnection("");
            db.ConnectionString = ConfigurationManager.ConnectionStrings["Provider=Microsoft.ACE.OLEDB.12.0;Data Source=countriesdb.accdb;"].ToString();
            db.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "SELECT Countries FROM countryofbirth";
            cmd.Connection = db;
            OleDbCommand dbc = new OleDbCommand(cmd.CommandText, db);
            OleDbDataReader read = dbc.ExecuteReader();

            birthcountrylist.DataSource = read;
            birthcountrylist.DataTextField = "Countries";       
            birthcountrylist.DataValueField = "Countries";
            birthcountrylist.DataBind();
            read.Close();
            db.Close();
        }
    }

I think my problem is with this part:

if($(this).val()=="US")

Thanks

我是新手编程。

I have a databound dropdownlist to a 2013 access db of a list of countries. I want to show/hide a division where there is a different textbox (where you fill your date of immigration) depending on your chosen country.

例子:

如果您选择英国作为出生国,文本框的日期将显示移民。但是如果你选择美国作为出生国,移民日期的文本框不会显示。

I thought to use jquery, but it's not working. How do I get the value of the chosen index of the dropdownlist?

代码:

jQuery:
$("#birthcountrylist").change(function)
{
if($(this).val()=="US") { $(divimmigratedate).hide(); }
else { $(divimmigratedate).show(); }
}); 

form:
<!-- Entering Birth Country /-->
<asp:DropDownList ID="birthcountrylist" runat="server" AutoPostBack="true" AppendDataBoundItems="true"></asp:DropDownList

<!--Entering Date of Immigration/-->
<div id="divimmigratedate" runat="server" style="display:none">
<asp:TextBox ID="dateimmigrate" runat="server" MaxLength="10" Width="13%" Text="" ></asp:TextBox>

back-code:
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            OleDbConnection db = new OleDbConnection("");
            db.ConnectionString = ConfigurationManager.ConnectionStrings["Provider=Microsoft.ACE.OLEDB.12.0;Data Source=countriesdb.accdb;"].ToString();
            db.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = "SELECT Countries FROM countryofbirth";
            cmd.Connection = db;
            OleDbCommand dbc = new OleDbCommand(cmd.CommandText, db);
            OleDbDataReader read = dbc.ExecuteReader();

            birthcountrylist.DataSource = read;
            birthcountrylist.DataTextField = "Countries";       
            birthcountrylist.DataValueField = "Countries";
            birthcountrylist.DataBind();
            read.Close();
            db.Close();
        }
    }

我想我的问题是这部分:

if($(this).val()=="US")

谢谢

answer1: 回答1:

It seems like there is a syntax error for change function.

jquery:

$("#birthcountrylist").change(function()
{
if($(this).val()=="US") { $(divimmigratedate).hide(); }
else { $(divimmigratedate).show(); }
}); 

它似乎有一个语法错误的变化函数。

jQuery:

$("#birthcountrylist").change(function()
{
if($(this).val()=="US") { $(divimmigratedate).hide(); }
else { $(divimmigratedate).show(); }
}); 
answer2: 回答2:

check the following code

 <script type="text/javascript">    
    $(document).ready(function () {
        $('#birthcountrylist').change(function () {
            if ($('#<%=birthcountrylist.ClientID %> option:selected').val() == "US") {
                alert("hello world1");
            }
            else {
                alert("hello world2");
            }
        })
    });       
</script> 

And here goes the HTML portion

 <asp:DropDownList ID="birthcountrylist" runat="server" AutoPostBack="true">
    <asp:ListItem Text="hello world 1" Value="US"></asp:ListItem>
    <asp:ListItem Text="hello world 2" Value="UK"></asp:ListItem>
</asp:DropDownList>

Let me know if it works , thanks ..

检查以下代码

 <script type="text/javascript">    
    $(document).ready(function () {
        $('#birthcountrylist').change(function () {
            if ($('#<%=birthcountrylist.ClientID %> option:selected').val() == "US") {
                alert("hello world1");
            }
            else {
                alert("hello world2");
            }
        })
    });       
</script> 

这是HTML部分

 <asp:DropDownList ID="birthcountrylist" runat="server" AutoPostBack="true">
    <asp:ListItem Text="hello world 1" Value="US"></asp:ListItem>
    <asp:ListItem Text="hello world 2" Value="UK"></asp:ListItem>
</asp:DropDownList>

让我知道,如果它的工作,谢谢..

jquery  asp.net