代码之家  ›  专栏  ›  技术社区  ›  Florian Müller

当前元素作为其事件函数param

  •  7
  • Florian Müller  · 技术社区  · 15 年前

    我有一个元素

    <input type="text">
    

    在这上面,有一个事件

    onChange="myfunction(param)".
    

    “param”是输入本身的内容。当我触发onChange(完成字段的更改)时,这个参数是这个字段的实际值,我该如何处理?

    有可能这样做吗:

    onChange="myfunction(document.getElementById('this_id'))"
    
    3 回复  |  直到 8 年前
        1
  •  22
  •   hunter    15 年前

    你可以通过 this myFunction 那将是 input

    <input type="text" onChange="myfunction(this)" />
    

    然后 我的功能 可能是这样的:

    function myFunction(obj)
    {
        var value = obj.value; // the value of the textbox
    }
    
        2
  •  2
  •   SLaks    15 年前

    在内联事件处理程序中, this 将引用DOM元素。

    因此,你可以写 onchange="myfunction(this)" 将DOM元素本身传递给函数。

        3
  •  2
  •   Nick Craver    15 年前

    为了得到 .value 内联,它看起来像这样:

    <input type="text" onchange="myfunction(this.value)" />