答案 0 :(得分:1)
你确实说过页面加载的时候,它就像这样简单......
<body onload="mylabel.innerHTML=filename.value;">
答案 1 :(得分:0)
我希望这是你想要的,我只是在功能之外添加了一行
Array.prototype.forEach.call(document.getElementsByName('filename'), function(elem) {
elem.addEventListener('change', function() {
document.getElementById('mylabel').innerHTML = this.value;
});
});
document.getElementById('mylabel').innerHTML = document.getElementById('filename').value;
<select class="select1" name="filename" id="filename">
<option>foo</option>
<option>bar</option>
</select>
<label id="mylabel"></label>
答案 2 :(得分:0)
Array.prototype.forEach.call(document.getElementsByName('filename'), function(elem) {
elem.addEventListener('change', function() {
document.getElementById('mylabel').innerHTML = this.value;
});
});
$('#mylabel').html($('#filename').val());
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select class="select1" name="filename" id="filename">
<option>foo</option>
<option>bar</option>
</select>
<label id="mylabel"></label>
&#13;
答案 3 :(得分:0)
为什么要循环?
let e = document.getElementById("filename");
let x = document.getElementById('mylabel');
document.addEventListener('DOMContentLoaded',function(){
x.innerHTML = e.value;
e.addEventListener('change', function(){
x.innerHTML = this.value;
});
});
&#13;
<select class="select1" name="filename" id="filename">
<option>foo</option>
<option>bar</option>
</select>
<label id="mylabel"></label>
&#13;
答案 4 :(得分:0)
应用DRY(不要自己重复):命名单击处理程序,然后在初始化期间手动调用它。或者,您也可以在初始化期间手动触发更改。
码
$(function() {
// event handler on change
$('#filename').change(mylabel_update);
// update mylabel to show filename
function mylabel_update() {
$('#mylabel').html($('#filename').value);
}
// initialize option A - call the function directly
mylabel_update();
// initialize option B - force the change event (don't need the named fn)
$('#filename').trigger('change');
});