Javascript Select Option not updating
Javascript Select Option not updating
我有一个下拉列表,当选择一个选项时,它需要用所选选项的值填充一个输入值,只是它不起作用,因为什么都没有改变。有人能看到并解释为什么要使用我的代码吗?
<select id="corr_sel" onchange="updatecredoff();">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<div id="cr_off"><?php echo $user_curr_coff;?></div>
function updatecredoff() {
if(value == 1) {
document.getElementById("corr_sel").value = '1';
document.getElementById("cr_off").innerHTML = '1';
}
else if(value == 2) {
document.getElementById("corr_sel").value = '2';
document.getElementById("cr_off").innerHTML = '2';
}
else if(value == 3) {
document.getElementById("corr_sel").value = '3';
document.getElementById("cr_off").innerHTML = '3';
}
else if(value == 4) {
document.getElementById("corr_sel").value = '4';
document.getElementById("cr_off").innerHTML = '4';
}
else if(value == 5) {
document.getElementById("corr_sel").value = '5';
document.getElementById("cr_off").innerHTML = '5';
}
else if(value == 6) {
document.getElementById("corr_sel").value = '6';
document.getElementById("cr_off").innerHTML = '6';
}
else if(value == 7) {
document.getElementById("corr_sel").value = '7';
document.getElementById("cr_off").innerHTML = '7';
}
else if(value == 8) {
document.getElementById("corr_sel").value = '8';
document.getElementById("cr_off").innerHTML = '8';
}
else if(value == 9) {
document.getElementById("corr_sel").value = '9';
document.getElementById("cr_off").innerHTML = '9';
}
else {
document.getElementById("corr_sel").value = '10';
document.getElementById("cr_off").innerHTML = '10';
}
};
在此之前,您还没有在代码中的任何位置定义value
(至少从您显示的内容来看):
if(value == 1) {
您可以在附加this
的情况下定义onchange="updatecredoff(this);
,然后在第一个条件赋值之前的函数中:
function updatecredoff(e) {
value = e.value;
示例:http://jsfiddle.net/niklasvh/yXHD5/
或者你可以通过这样的ID来获得值:
value = document.getElementById("corr_sel").value;
如果这就是你打算用这个功能做的所有功能,你可以把它简化为:
function updatecredoff(e) {
document.getElementById("cr_off").innerHTML = e.value;
};
var"Value"来自哪里,您还没有在代码中初始化它。
你的代码会像这样正常工作。
document.getElementById("cr_off").innerHTML = document.getElementById("corr_sel").value;
除了没有定义value
之外,您的函数看起来非常多余。从select
元素中选择一个项会更改其selectedIndex
属性,随后可以使用该属性检索值并将其写入文本输入字段。例如:
function updatecredoff(el) {
document.getElementById('cr_off').value = el.options[el.selectedIndex].value;
};
下面是一个使用的jsfiddle
相关文章:
- jQuery is not loaded
- AngularJS JSON not arriving php
- $rootScope not working
- reactRedux is not defined
- jQuery document.ready not working
- Javascript/Jquery Blob not showing Chrome PDF
- ReferenceError: not defined
- ReferenceError: cordova is not defined @ng-cordova.min.js:7
- Javascript getElementsByTagName not working?
- jQuery's trim()的前缀为not运算符
- ngDialog not updating $scope
- fetch is not updating collection.model.models
- DOM not updating
- jQuery $(window).resize Chrome not updating
- AngularJS not updating?
- innerHTML not updating
- Jquery mobile Collabsible Listview not updating
- textContent not updating HTML
- Javascript Select Option not updating
- ngFor not updating in IE 11 (RC4 with CoreJS)