如何使用jQuery检查文本框中的文本是否 asp.net 选中

How to check if text in asp.net textbox is selected with jQuery?

本文关键字:文本 是否 asp net 选中 何使用 jQuery 检查      更新时间:2023-09-26

我想知道这是否可能,我想检查用户是否在文本框控件内选择了文本,就像这个解决方案一样(但这仅适用于 html 输入控件,而不是 asp.net 服务器控件)

就该示例而言,您需要nothing to change服务器控制except the syntax

<asp:TextBox id="txt1" runat="server" value="Selected?"><asp:TextBox>
<asp:Button id="btnSubmit" runat="server" OnClientClick="alert(isTextSelected();" ></asp:Button>
    function isTextSelected() {
input = document.getElementById('<%= test.ClientID %>'))
        if (typeof input.selectionStart == "number") {
            return input.selectionStart == 0 && input.selectionEnd == input.value.length;
        } else if (typeof document.selection != "undefined") {
            input.focus();
            return document.selection.createRange().text == input.value;
        }
    }

你可以简单地通过使用来实现这一点

$('select, input').click(function(){
   $(this).removeClass('disabled');
});