如何使用jQuery访问输入字段文本?

Sai*_*udo 0 javascript jquery

我正在尝试使用jQuery的typeWatch插件.我有这个javascript:

 <script type="text/javascript">
 $(document).ready(function() {
   var options = {
        callback: function() { alert("a-ok!  " + $(this).text); },
        wait: 333,
        highlight: true,
        captureLength: 1
    }

    $("#customer").typeWatch(options);
 });
 </script>
Run Code Online (Sandbox Code Playgroud)

我的观点的HTML有这个:

 Method B: <%= Html.TextBox("customer") %>
Run Code Online (Sandbox Code Playgroud)

通过此测试,如果我在文本框中键入"ABC",我希望弹出"a-ok!ABC"的警报.相反,以下显示......

a-ok!  function (F) {
    if (typeof F !== "object" && F != null) {
        return this.empty().append((this[0] && this[0].ownerDocument || 
               document).createTextNode(F));
    }
    var E = "";
    o.each(F || this, function () {o.each(this.childNodes, function () {
          if (this.nodeType != 8) {
             E += this.nodeType != 1 ? this.nodeValue : o.fn.text([this]);}});});
   return E;
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试将$(this).text更改为.val,并且我还尝试将输入字段更直接地引用为$("#customer"),但它们会产生类似的结果.如何只访问输入的文本?

Rop*_*tah 5

您应该在文本框上使用val()函数:

$(this).val();
Run Code Online (Sandbox Code Playgroud)