我制作的脚本无法正常工作,我找不到问题

时间:2018-09-20 19:29:13

标签: javascript html

首先,对不起,我知道下一个代码是错误的,我是编程新手,但我遇到了问题。当用户从“选择”中选择一个选项时,必须更改其值,然后,用户必须以数字形式编写一个价格,该价格不得高于前一个值,这是脚本应该起作用但不起作用的时候。正确的代码必须是这样的:

<script>
function cboc(){ 
        var cb = $("#cbotipfon").val();
        var maxvalGORE = null;

        if(cb == 0){
            maxvalGORE = 0;
        if(cb == 15){
            maxvalGORE = 100;       
        }
        if(cb == 16){
            maxvalGORE = 200;
        }
} 

function cbocc(){ 
    var val = null;
    var x = document.GetElementById('txtprepos').value;

    val = parseInt(x); 

    if(val > maxvalGORE){
    alert('The value is higher than $' + maxvalGORE +'.'); 
    document.GetElementById('txtprepos').value = "";
    }    
}    
</script>

<select style="width=5.5em;" name="cbotipfon" id="cbotipfon" onchange="cboc()">
                    <option value="0">Select</option>                                      
                            <option value="15">Option A</option>
                            <option value="16">Option B</option>                                  
                </select>

<input type="number" onblur="cbocc()" name="txtprepos" id="txtprepos"/>

我一直困扰着这个问题好几天了。如果您能帮助我,我将永远感激不已。预先感谢您,抽出宝贵时间。

1 个答案:

答案 0 :(得分:0)

这是一个简单的例子。无需声明任何功能。只需正确声明变量并使用addEventListener方法即可获取inputselect元素的值。

var maxvalGORE ,val ,cb ,x = null;
document.getElementById("cbotipfon").addEventListener("change", function(){
        var cb = document.getElementById('cbotipfon').value;
        if(cb == 0){
            maxvalGORE = 0;
            }
        if(cb == 15){
            maxvalGORE = 100;       
        }
        if(cb == 16){
            maxvalGORE = 200;
        }
});

document.getElementById("txtprepos").addEventListener("change", function(){
    x = document.getElementById('txtprepos').value;
    val = parseInt(x); 
    if(val > maxvalGORE){
    alert('The value is higher than $' + maxvalGORE +'.'); 
    } else if(val == maxvalGORE) {
    alert('The value is equal to $' + maxvalGORE +'.');     
    } else {
    alert('The value is lower than $' + maxvalGORE +'.');    
    }
    document.getElementById('txtprepos').value = ""; 
});
<select style="width=5.5em;" name="cbotipfon" id="cbotipfon">
                    <option value="0">Select</option>                                      
                            <option value="15">Option A</option>
                            <option value="16">Option B</option>                                  
                </select>

<input type="number" name="txtprepos" id="txtprepos"/>