Js

时间:2019-11-01 12:04:58

标签: javascript jquery html

我正在尝试将多个滑块范围输入添加到我的网页,我希望当我使用滑块时其同级输入的值会更改。但是我发现只有第一个滑块有效,而其他滑块无效

<div class="form-group my-3 d-flex flex-column">
                                    <div class="d-flex justify-content-between align-items-center simulateur mb-3">               
                                        <label for="formGroupExampleInput" class="important">Revenus nets/mois* <i class="fas fa-info-circle"></i></label>
                                        <input type="text" id="revenue" class="form-control"  placeholder="" value="0"> 
                                    </div>
                                    <input id="RVN" type="range" value="0" min="0" max="7000" step="100"/>
                                </div>


                                <div class="form-group my-3 d-flex flex-column">
                                    <div class="d-flex justify-content-between align-items-center simulateur mb-3">               
                                        <label for="formGroupExampleInput" class="important">Prêt en cours/mois* <i class="fas fa-info-circle"></i></label>
                                        <input type="text" id="pret" class="form-control"  placeholder="" value="0">    
                                    </div>
                                    <input id="val_pret" type="range" value="0" min="0" max="7000" step="100"/>
                                </div>


                                <div class="form-group my-3 d-flex flex-column">
                                    <div class="d-flex justify-content-between align-items-center simulateur mb-3">               
                                        <label for="formGroupExampleInput" class="important">Mensualité souhaitée* <i class="fas fa-info-circle"></i></label>
                                        <input type="text" id="mensualite" class="form-control"  placeholder="" value="0">  
                                    </div>
                                    <input id="val_mensualite" type="range" value="0" min="0" max="1000000000000" step="10000"/>
                                </div>


                                <div class="form-group my-3 d-flex flex-column">
                                    <div class="d-flex justify-content-between align-items-center simulateur mb-3" >                  
                                        <label for="formGroupExampleInput" class="important" disabled>Apport personnel <i class="fas fa-info-circle"></i></label>
                                        <input type="text" id="apport_personnel" class="form-control"  placeholder="" value="0">    
                                    </div>
                                    <input id="val_apport" type="range" value="0" min="0" max="7000" step="100" />
                                </div>


                                <div class="form-group my-3 d-flex flex-column">
                                    <div class="d-flex justify-content-between align-items-center simulateur mb-3">               
                                        <label for="formGroupExampleInput" class="important">Durée du prêt <i class="fas fa-info-circle"></i></label>
                                        <input type="text" id="duree" class="form-control"  placeholder="" value="0">   
                                    </div>
                                    <input id="val_duree" type="range" value="0" min="0" max="60" step="5"/>
                                </div>

这就是我的js函数,我对所有函数都使用相同的方式,但只有第一个有效?

var i= document.getElementById('RVN');
var o = document.getElementById('revenue');
    o.value = i.value;
i.addEventListener('input', function(){
    o.value = i.value;
}, false);


var p= document.getElementById('pret');
var vp = document.getElementById('val_pret');
    vp.value = p.value;
p.addEventListener('input', function(){
    vp.value = p.value;
}, false);


var m= document.getElementById('mensualite');
var vm = document.getElementById('val_mensualite');
    vm.value = m.value;
m.addEventListener('input', function(){
    vm.value = m.value;
}, false);


var d= document.getElementById('duree');
var vd = document.getElementById('val_duree');
    vd.value = d.value;
d.addEventListener('input', function(){
    vd.value = d.value;
}, false);

DEMO

1 个答案:

答案 0 :(得分:2)

您正在为输入文本呼叫侦听器

var p= document.getElementById('pret');//input-text
var vp = document.getElementById('val_pret');//input-range
    vp.value = p.value;
p.addEventListener('input', function(){ //here is your error
    vp.value = p.value;
}, false);

您应该调用输入范围的列表器,这是修改后的代码

var i= document.getElementById('RVN');
var o = document.getElementById('revenue');
    o.value = i.value;
i.addEventListener('input', function(){
    o.value = i.value;
}, false);


var p= document.getElementById('val_pret');// input-range
var vp = document.getElementById('pret');// input-text
    vp.value = p.value;
p.addEventListener('input', function(){//solution
    vp.value = p.value;
}, false);


var m= document.getElementById('val_mensualite');
var vm = document.getElementById('mensualite');
    vm.value = m.value;
m.addEventListener('input', function(){
    vm.value = m.value;
}, false);