开发者

Javascript Select Option not updating

I have a dropdown which when an option is selected it needs to populate an input value with the value of the option selected only its not working as in nothings changing, Could anybody see and explain why going of my code?

 <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';
        documen开发者_如何学JAVAt.getElementById("cr_off").innerHTML = '10';
    }
    };


You haven't defined value anywhere in your code (at least from what you've shown) before this:

if(value == 1) {

You could define onchange="updatecredoff(this); with this attached, and then in your function before your first conditional assign the value:

function updatecredoff(e) {
    value = e.value;

Example: http://jsfiddle.net/niklasvh/yXHD5/

Or you could just get the value by the ID like this:

value = document.getElementById("corr_sel").value;

If that is all the functionality you intend to do with the function, you could simplify it to just:

function updatecredoff(e) {
    document.getElementById("cr_off").innerHTML = e.value;   
};


where did the var "Value" come from, you have not initialised it in the code.

You're code will work just fine like this.

 document.getElementById("cr_off").innerHTML = document.getElementById("corr_sel").value;


Apart from the fact that value isn't defined, your function looks very redundant. Selecting an item from the select element changes its selectedIndex property, which you can subsequently use to retrieve the value and write it to a text input field. E.g. like this:

function updatecredoff(el) {
    document.getElementById('cr_off').value = el.options[el.selectedIndex].value;
};

Here's a jsfiddle to play with

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜