开发者

May i change all the <input> at the time?

开发者 https://www.devze.com 2023-04-11 02:50 出处:网络
开发者_JS百科May i know how can i batch change a input for other input at javascript example: <input type=\"text\" id=\"abc\">`<input type=\"text\" id=\"abc1\">`<input type=\"text\" id

开发者_JS百科May i know how can i batch change a input for other input at javascript example:

<input type="text" id="abc">`<input type="text" id="abc1">`<input type="text" id="abc2">`

<input type="text" id="abc3">`<input type="text" id="abc4">`

the following abc1->abc4 will change according the 1st.


This will get all the inputs in the document and change them to the value of the first.

var masterInput = document.getElementById('abc');
masterInput.onchange = function () {
    var inputs = document.getElementsByTagName('input');
    for (var i = 1; i < inputs.length; i++) {
        inputs[i].value = this.value;
    }
};

If you have other inputs in the page (quite likely), you can replace document with the parent element, or you can iterate through the ids individually:

var masterInput = document.getElementById('abc');
masterInput.onchange = function () {
    var inputs = ['abc1', 'abc2', 'abc3', 'abc4'];
    for (var i = 0; i < inputs.length; i++) {
        document.getElementById(inputs[i]).value = this.value;
    }
};
0

精彩评论

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