javascript:当另一个输入值改变时如何清理输入框?

时间:2012-07-15 07:14:52

标签: javascript

我在另一个输入(#parent)上做了一个输入(#child)自动建议。当#parent输入值改变时,我想清除#child的值。我想学习如何做到这一点,谢谢!

1 个答案:

答案 0 :(得分:0)

您可以订阅父级的onchange事件,然后清除子级的值:

window.onload = function() {
    // the document has loaded => we could access the DOM
    // we subscribe to the onchange event pf the parent:
    document.getElementById('parentId').onchange = function() {
        // and when this event is triggered we clean the child
        // element value
        document.getElementById('childId').value = '';
    };
};