Make form elements onchange bubble in Internet Exp

2019-07-17 18:06发布

问题:

How do you make the onChange event of various form elements bubble up to the parent form object in Internet Explorer? When select boxes, radio button, ... almost anything ... changes in IE, the parent form's onChange is not fired. My forms are dynamically changing so it will be hard to hack it by listening on each and every child form element.

HTML

<form id="myForm">
 <select>
  <option>uno</option>
  <option>dos</option>
 </select>
 <input type="radio" name="videoDevice" value="tres" checked="checked" />
 <input type="radio" name="videoDevice" value="cuatro" />
</form>

JS

$('myForm').observe(
 'change', 
 function() {
  // this only runs in non-IE browsers
  alert('the form changed');
 }
);

BTW, I'm using the Prototype framework. Shouldn't it have handled the cross-browser differences for me?

回答1:

I had to use a hack for IE. I manually made all the child form elements trigger the onChange callback rather than have the parent form do it. I wish there was a cleaner solution.

$('myForm').select('input', 'radio', 'textarea').each(
 function(formElement) {
  formElement.observe(
   'change', 
   function() {
    alert('changed');
   }
  );
 }
);


回答2:

I think you might have a simple syntax error in your code:

$('myForm') should be $('#myForm')