【发布时间】:2014-12-02 17:27:43
【问题描述】:
我的 ASP.net 页面中有以下内容:
<input type="text" placeholder="Search..." size="30" id="pageLink" />
<br />
<span id="imgGo" class="imgGo floatRight"></span>
JQuery:
$('.imgGo').click(function () {
var strString = $("#pageLink").val()
var strSearch = "http://www.google.com/search=";
var url = strSearch + strString;
window.open(url, '_blank');
return false;
});
$("#pageLink").keyup(function (event) {
if (event.keyCode == 13 || event.which == 13) {
$(".imgGo").click();
}
});
当我在文本框中输入内容并单击 imgGo 范围时,它可以正常工作,但如果我按 Enter 键,则会触发位于另一个面板中的搜索按钮。
如何修改我的 JQuery 代码以确保不会发生这种情况,或添加代码隐藏以确保仅当在 pageLink 文本框具有焦点时按下输入按钮时才会触发 imgGo?
我尝试了以下方法,但没有成功,因为 imgGo 不是按钮:
<asp:Panel ID="pnlMedSearch" runat="server" DefaultButton="GoImg">
<!--<input type="text" placeholder="Search..." size="30" id="p2" />-->
<asp:TextBox Width="30" ID="pageLink" runat="server" />
<br />
<!--<span id="imgGo" class="imgGo floatRight"></span>-->
<asp:Label ID="imgGo" CssClass="imgGo floatRight" runat="server" />
</asp:Panel>
我收到以下错误:
The DefaultButton of 'pnlMedSearch' must be the ID of a control of type IButtonControl.
我想使用不是 ASP.net 控件的文本框,因为 placeholder 属性对我来说很重要。
【问题讨论】:
-
其他面板中的搜索按钮是否有
imgGo类?我注意到您正在通过 jQuery 定位.imgGo。这将触发任何匹配元素的事件,您可能希望基于唯一的id来执行此操作。 -
imgGo仅用于此实例。 -
您的错误信息有点奇怪
The DefaultButton of 'pnlMedSearch' must be the ID of a control of type IButtonControl... 您是否尝试使用标签作为默认按钮?在您的示例 HTML 中,您已将默认按钮设置为GoImg,那么为什么不将<asp:Button>添加到 ID 为GoImg的面板中呢?这应该工作...... -
我需要使用
placeholder作为我在ASP.net中无法使用的文本框 -
您绝对可以在
<asp:TextBox />上使用placeholder=""属性。例如<asp:TextBox Width="30" ID="pageLink" runat="server" placeholder="Foo" />