检查时JS返回未定义,并更改选择的选项

时间:2013-02-19 21:36:08

标签: javascript select checkbox undefined option

我有一个HTML JS代码:

<form action="/resize" method="GET" name="resolutionForm">
 <select name="res" id="res" onchange="checkzoom(this.value); checkcut('centercropcheck')">
  <option value="original"> Original</option><option value="800x600">800x600</option>
 </select>
 <input type="checkbox" id="stretchcheck" name="stretch" onclick="verificastretch(this.id);">Esticar
 <div id="results">
  <span>original: </span>
  <span>200x200</span>
  <span>zoom: </span>
  <span>0x</span>
  <span id="zoom">0x</span></span>
 </div>
</form>
<script type="text/javascript">
 function elembyid(id){
    return document.getElementById(id);
 }
 function checkzoom(v){
    if(v == '200x200' || v == 'original'){
        var zoom = '0x';
    }else{
        if(elembyid('stretchcheck').checked){
            verificastretch('stretchcheck');
        }else{
            var novow = Number(v.substr(0,v.lastIndexOf("x")));
            var novoh = Number(v.substr(v.lastIndexOf("x") +1, v.length));
            //alert('novow'+novow+'novoh'+novoh);
            var tamzoomw = novow / 200;
            var tamzoomh = novoh / 200;
            if(tamzoomw < tamzoomh){
                var zoom = (tamzoomw.length > 3? tamzoomw.toFixed(2) : tamzoomw)+'x';
            }else{
                var zoom = (tamzoomh.length > 3? tamzoomh.toFixed(2) : tamzoomh)+'x';
            }
        }
        elembyid('zoom').innerHTML = zoom;
    }
 }
 function verificastretch(id){
    var v = elembyid('res').value;
    if(v == '200x200' || v == 'original'){
        checkzoom(v);
    }else{
        if(elembyid(id).checked){
            //alert('aqui');
            var novow = Number(v.substr(0,v.lastIndexOf("x")));
            var novoh = Number(v.substr(v.lastIndexOf("x") +1, v.length));
            //alert('novow'+novow+'novoh'+novoh);
            var tamzoomw = novow / 200;
            var tamzoomh = novoh / 200;
            var zoom = (tamzoomw.length > 3? tamzoomw.toFixed(2) : tamzoomw)+'x '+(tamzoomh.length > 3? tamzoomh.toFixed(2) : tamzoomh)+'x';
            alert(zoom);
            elembyid('zoom').innerHTML = zoom;
        }else{
            checkzoom(v);
        }
    }
 }
</script>

当选中拉伸的复选框,并且我更改了选择res的所选选项时,我在div id =“result”中得到了未定义,我该如何解决这个问题? 在文件中,代码没有这样的';'因为复制时发生了错误。

1 个答案:

答案 0 :(得分:0)

if-condition

之后
        if(elembyid('stretchcheck').checked){
            verificastretch('stretchcheck');
        }else{
            …
        }

已完成且verificastretch已将范围zoom正确设置为4x 3x,范围zoomundefined变量zoom覆盖:< / p>

        elembyid('zoom').innerHTML = zoom;

我认为你想在else子句中使用该行。

您可以通过使用调试器并从函数checkzoom开始逐步执​​行代码来找到这一点。