【问题标题】:How to check if text in asp.net textbox is selected with jQuery?如何检查是否使用 jQuery 选择了 asp.net 文本框中的文本?
【发布时间】:2012-08-11 11:32:21
【问题描述】:

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

【问题讨论】:

  • 不要太惊讶,但在幕后几乎所有的 asp.net 控件都呈现为 html 控件。因此,您完全可以免费使用您所指的解决方案。

标签: javascript jquery asp.net


【解决方案1】:

就该示例而言,您需要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;
        }
    }

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-22
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 2018-10-23
      • 1970-01-01
      相关资源
      最近更新 更多