简体   繁体   English

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

[英]JQuery get Value of an Element: onfocus=“inputInlineHint(self);”

i have an input field which calls the function "inputInlineHint(self)" on blur and on focus. 我有一个输入字段,在模糊和焦点上调用函数“inputInlineHint(self)”。 It also passes self... 它也通过自我......

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

Now i'd like to retrieve the current value of the field: 现在我想检索该字段的当前值:

function inputInlineHint(e) {

  var theValue = '';

  //Now i need to retrieve the value... but how?
}

I hope you guys can help me with that... should be pretty basic but i'm new to jquery. 我希望你们可以帮助我...应该是非常基本但我是jquery的新手。

Thanks in advance! 提前致谢!

  1. Pass this not self . 通过this不是self self is undefined. self是不确定的。
  2. Rename the variable from e to something else. 将变量从e重命名为其他内容。 e is traditionally used to receive an event object, but you aren't assigning the function as an event handler e传统上用于接收事件对象,但您没有将该函数指定为事件处理程序
  3. whatever_you_renamed_e_to.value

Seeing as you've tagged your question with jQuery, the jQuery solution would be something like this: 看到你用jQuery标记了你的问题,jQuery解决方案将是这样的:

$(document).ready(function() {
    $('#one').blur(function() {
        var val = $(this).val();
        // rest of processing, etc.   
    })
    .focus(function() {
        var val = $(this).val();
        // rest of processing, etc.
    });    
});

You are much better off using a jQuery binding function as opposed to writing the script name into the control: 使用jQuery绑定函数要好得多,而不是将脚本名称写入控件:


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM