在JavaScript中获取多个文本框ID

时间:2013-02-01 04:22:08

标签: javascript asp.net textbox focus placeholder

我这里的代码是ASP.NET格式

<asp:TextBox ID="BuildingNameTextBox" runat='server' placeholder="Building Name" Text='<%# Bind("BuildingName") %>' />
<asp:TextBox ID="AddressTextBox" runat="server" placeholder="Example: 8th Floor, Sun Life Centre, 5th Avenue corner Rizal Drive, Fort Bonifacio Taguig City" Text='<%# Bind("Address") %>' />
<asp:TextBox ID="CityTextBox" placeholder="Use the autocomplete." runat="server" Text='<%# Bind("City") %>' />
<asp:TextBox ID="RegionTextBox" placeholder="Use the autocomplete." runat="server" Text='<%# Bind("Region") %>' class="subMarkets"/>

此代码在javascript中

$("#BuildingNameTextBox").data("holder", $("#BuildingNameTextBox").attr("placeholder"));
            $("input").focusin(function () {
                $(this).attr("placeholder", "");
            });
            $("input").focusout(function () {
                $(this).attr("placeholder", $(this).data("holder"));
            });

javascript的功能是在文本框处于焦点时删除占位符,并在不在焦点时恢复相应文本框的文本 我似乎找不到恢复后续文本框的相应文本的方法。

E.g。当我选择Building文本框时,文本框的下一个占位符将替换为BuildingTextBox(Building)的占位符。

我需要找到一种方法让占位符还原到相应的文本框。谢谢

2 个答案:

答案 0 :(得分:0)

添加一个在文档就绪上调用的方法,填充所有这些方法,而不仅仅是#buildingnametextbox:

$("input").each(function () {
    $(this).data("holder", $(this).attr("placeholder"));
});

OR

在您的标记中,添加数据值:

<asp:TextBox ID="BuildingNameTextBox" runat='server' data-holder="Building Name" placeholder="Building Name" Text='<%# Bind("BuildingName") %>' />
<asp:TextBox ID="AddressTextBox" runat="server" data-holder="Example: 8th Floor, Sun Life Centre, 5th Avenue corner Rizal Drive, Fort Bonifacio Taguig City" placeholder="Example: 8th Floor, Sun Life Centre, 5th Avenue corner Rizal Drive, Fort Bonifacio Taguig City" Text='<%# Bind("Address") %>' />
<asp:TextBox ID="CityTextBox" data-holder="Use the autocomplete." placeholder="Use the autocomplete." runat="server" Text='<%# Bind("City") %>' />
<asp:TextBox ID="RegionTextBox" data-holder="Use the autocomplete." placeholder="Use the autocomplete." runat="server" Text='<%# Bind("Region") %>' class="subMarkets"/>

答案 1 :(得分:0)

输入处于焦点时,占位符将被删除,如果输入任何文本,它将不再显示占位符!

因此,即使每次“#buildingnametextbox”失去焦点时都更改占位符值,它也将毫无用处,因为您不会使用它。我们假设如果用户将该字段留空......这也会清空占位符,我认为您不希望这种情况发生:)