You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决$write.val is not a function及write is not a function错误?

解决 write is not a function 和 $write.val is not a function 错误

我来帮你逐个拆解这两个错误,一步步解决问题:

1. 错误:write is not a function

原因

在你的补充代码中,你写了var write = write();——此时write函数还没有被定义,就直接尝试调用它,浏览器自然会抛出“write不是函数”的错误。你需要先定义write这个对象/函数,再调用它的init方法。

修复方法

创建一个包含init方法的write对象,同时定义你需要的属性(比如currentElement、currentElementCursorPosition):

// 先定义write对象
var write = {
  init: function(keyboardId) {
    // 这里可以放初始化键盘的逻辑,比如绑定事件等
    console.log(`Keyboard initialized with id: ${keyboardId}`);
  },
  currentElement: null,
  currentElementCursorPosition: 0
};

$(function () {
  write.init("keyboard"); 
  // 第一个输入框获取焦点
  var $firstInput = $(":input").first().focus();
  write.currentElement = $firstInput;
  write.currentElementCursorPosition = 0;
});

2. 错误:$write.val is not a function

原因

在原代码的focus事件绑定中,你写了$write = $(this).attr('id');——这行代码把原本是jQuery对象的$write,替换成了当前输入框的id字符串。字符串类型没有.val()这个方法,所以后面调用$write.val()时就会报错。

修复方法

把$write保留为jQuery对象,而不是存储id值。修改focus事件中的赋值逻辑:

// 原错误代码
// $write = $(this).attr('id');

// 修正后的代码
$write = $(this); // 保留jQuery对象,而不是id字符串

同时,原代码中还有一些语法错误(比如多余的闭合括号)和逻辑问题(比如$write.html(txt)中txt未定义),我帮你整理了完整的修正代码:

$(function() {
  var $write = $(":input").first(); // 初始化$write为第一个输入框的jQuery对象
  let shift = false, capslock = false;

  // 绑定输入框的focus事件
  $(":input").bind('focus', function() {
    $write = $(this); // 更新$write为当前聚焦的输入框jQuery对象
  });

  $('#keyboard li').click(function() {
    var $this = $(this), character = $this.html();

    // Shift键逻辑
    if ($this.hasClass('left-shift') || $this.hasClass('right-shift')) {
      $('.letter').toggleClass('uppercase');
      $('.symbol span').toggle();
      shift = !shift;
      capslock = false;
      return false;
    }

    // Caps Lock键逻辑
    if ($this.hasClass('capslock')) {
      $('.letter').toggleClass('uppercase');
      capslock = true;
      return false;
    }

    // 删除键逻辑
    if ($this.hasClass('delete')) {
      var currentVal = $write.val();
      var newVal = currentVal.substr(0, currentVal.length - 1);
      $write.val(newVal);
      $write.autocomplete("search", newVal);
      return false;
    }

    // 特殊字符处理
    if ($this.hasClass('symbol')) character = $('span:visible', $this).html();
    if ($this.hasClass('space')) character = ' ';
    if ($this.hasClass('tab')) character = "\t";
    if ($this.hasClass('return')) character = "\n";

    // 大写字母处理
    if ($this.hasClass('uppercase') || (shift && !capslock)) {
      character = character.toUpperCase();
    }

    // 点击按键后取消shift状态
    if (shift === true) {
      $('.symbol span').toggle();
      if (capslock === false) $('.letter').toggleClass('uppercase');
      shift = false;
    }

    // 追加字符到输入框
    var currentVal = $write.val();
    $write.val(currentVal + character);
    $write.autocomplete("search", currentVal + character);
  });
});

额外说明

  • 我移除了原代码中多余的注释和闭合括号,避免语法错误;
  • 统一用.val()来操作输入框的值(输入框应该用.val()而不是.html(),.html()更多用于富文本元素);
  • 修正了删除键和追加字符的逻辑,确保变量定义后再使用。

内容的提问来源于stack exchange,提问作者Vivek Pandey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:19:10