以下のすべての例は、修正された HTML に基づいていることに注意してください(<option>
タグは で閉じられており</option>
、タグではなく、</input>
タグで閉じられている要素はありません</input>
)。
A:
<select id="inputsa">
<option id="inputa" name="input[1][]">A</option>
<option id="inputb" name="input">B</option>
<option id="inputc" name="input[2][]">C</option>
</select>B:
<select id="inputsb">
<option id="inputa" name="input[1][]">A</option>
<option id="inputb" name="input">B</option>
<option id="inputc" name="input[2][]">C</option>
</select>C:
<select id="inputsc">
<option id="inputa" name="input[1][]">A</option>
<option id="inputb" name="input">B</option>
<option id="inputc" name="input[2][]">C</option>
</select>
<br>
<input id="copyAtoB" type="button" value="Copy A to B" data-from="a" data-to="b">
<br>
<input id="copyBtoC" type="button" value="Copy B to C" data-from="b" data-to="c">
1 つのアプローチ:
function copyValue(from,to){
/* these lines check if the supplied variables are Objects, with a
typeof check, if they *are* they're assumed to be jQuery objects
and that object is retained in the variable; otherwise it's assumed
to be the String of an id, and a jQuery object is created, and
assigned to the variable. */
from = typeof from == "object" ? from : $('#' + from);
to = typeof to == "object" ? to : $('#' + to);
// gets the selected option, and copies that to the other node/object
to[0].selectedIndex = from[0].selectedIndex;
}
$('#copyAtoB').click(function(e){
e.preventDefault();
copyValue($('#inputsa'),$('#inputsb'));
});
$('#copyBtoC').click(function(e){
e.preventDefault();
copyValue($('#inputsb'),$('#inputsc'));
});
JS フィドルのデモ。
整頓された、もう少し一般的な jQuery オプション:
function copyValue(from, to) {
// same as above
from = typeof from == "object" ? from : $('#' + from);
to = typeof to == "object" ? to : $('#' + to);
to[0].selectedIndex = from[0].selectedIndex;
}
$('input:button').click(function (e) {
e.preventDefault();
// retrieves the data from the current jQuery object
var data = $(this).data(),
// gets the 'from' data (where you're copying from)
f = 'inputs' + data.from,
// gets the 'to' data (where you're copying to)
t = 'inputs' + data.to;
/* calls the function, demonstrating passing both a jQuery object,
and a String equal to the relevant id */
copyValue($('#' + f), t);
});
JS フィドルのデモ。
そして、単純な JavaScript がとても好きだからです。
function copyValue(from,to, prefix){
// sets a default prefix to be used
prefix = prefix || 'inputs';
/* checks if the variables are element nodes, if they are that node
is used. Otherwise we assume an id is passed, and use that to find
the relevant element node */
from = from.nodeType == 1 ? from : document.getElementById(prefix + from);
to = to.nodeType == 1 ? to : document.getElementById(prefix + to);
to.selectedIndex = from.selectedIndex;
}
// gets all input elements
var inputs = document.getElementsByTagName('input');
// iterates through those input elements, and...
for (var i = 0, len = inputs.length; i<len; i++){
// if they have a type equal to 'button'...
if (inputs[i].type == 'button') {
// binds a click-handler
inputs[i].onclick = function(e){
// gets the values from 'data-from' and 'data-to' attributes
var f = this.getAttribute('data-from'),
t = this.getAttribute('data-to');
// assigns the function to be called, with the arguments
copyValue(f,t, 'inputs');
};
}
}
JS フィドルのデモ。
参考文献: