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

标签 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>

我的 View 的 HTML 是这样的:

 Method B: <%= Html.TextBox("customer") %>

通过此测试,如果我在文本框中输入“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;
}

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

最佳答案

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

$(this).val();

关于javascript - 如何使用 jQuery 访问输入字段文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/944697/

相关文章:

jquery - 将 Json 数据传递给 MVC Controller - Action 方法对象参数

javascript - 如何过滤表格并仅显示 <td> 值而不是 <tr> 值?

javascript - 使用 javascript 将 [no-cache] 重定向到页面

javascript - 使用时间作为 css 背景色

javascript - 有没有一种优雅的方法可以在 foreach() 之后链接 join() ?

javascript - 背景不向左重复

javascript - 如何将 Javascript 函数的计算显示到 HTML 上?

jquery - 不正确的 Sequelize 查询

javascript - 具有多种条件的 Mongoose 中的 $gte 和 $lte

javascript - 提取大写字母(英文) |首选方法