开发者

javascript input field onchange causes "is not a function"

I have a function i would like to run onChange. This works fine with an a-tag. But when i use it on an input-tag it just says:

form.saveFile is not a function

This works:

<a onclick="form.saveFile('fec056b774ffefa479c7dd3a632275cb','fec056b774ffefa479c7dd3a632275cb_filetest'); return false;" href="#">开发者_JS百科Upload file</a>

This does NOT work:

<input onchange="form.saveFile('fec056b774ffefa479c7dd3a632275cb','fec056b774ffefa479c7dd3a632275cb_filetest');" id="fec056b774ffefa479c7dd3a632275cb_filetest" class="file" type="file"  name="fec056b774ffefa479c7dd3a632275cb_filetest" accesskey="F"/>

Both calls the same form.saveFile(); function.

The form variable is declared in a included js file like this:

form = {
  version: '1.0.0',
  ...alot of functions... 
  saveFile : function(callback,p){
    .................
  }
}


form is a reserved word in a onchange handler. Just rename your variable and it will work:

var frm = {
    version: '1.0.0',
    saveFile : function(callback,p){
    }
};

<input onchange="frm.saveFile('fec056b774ffefa479c7dd3a632275cb','fec056b774ffefa479c7dd3a632275cb_filetest');" id="fec056b774ffefa479c7dd3a632275cb_filetest" class="file" type="file"  name="fec056b774ffefa479c7dd3a632275cb_filetest" accesskey="F"/>


Inside forms form does refer to that specific form element. So you need to distinguish between the form that’s referring to the form element and your form variable. You can do so with using window.form if you defined your form variable in the global scope. Or you just rename it.


need more info. did you try in all browsers? it should work. If the text has been changed, once the focus leaves the text box onchange will occur. try using alert() to test if the function is really called. there could be something wrong with your function. hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜