JQuery获取元素的值:onfocus ="inputInlineHint(self);"

n00*_*00b 3 javascript jquery

我有一个输入字段,在模糊和焦点上调用函数"inputInlineHint(self)".它也通过自我......

<input type="text" id="one" class="textbox" value="Your Name" onfocus="inputInlineHint(self);" onblur="inputInlineHint(self);" />
Run Code Online (Sandbox Code Playgroud)

现在我想检索该字段的当前值:

function inputInlineHint(e) {

  var theValue = '';

  //Now i need to retrieve the value... but how?
}
Run Code Online (Sandbox Code Playgroud)

我希望你们可以帮助我...应该是非常基本但我是jquery的新手.

提前致谢!

Que*_*tin 5

  1. this不通过self.self未定义.
  2. 将变量重命名e为其他内容.e传统上用于接收事件对象,但您没有将该函数指定为事件处理程序
  3. whatever_you_renamed_e_to.value


Ed *_*mes 5

使用 jQuery 绑定函数比将脚本名称写入控件要好得多:

$(function() {
   $('#one').focus(function() {
      var value = $(this).val();  /// this is now the value
   }).blur(function (){
      var value = $(this).val();  /// same again
   );
);
Run Code Online (Sandbox Code Playgroud)