简体   繁体   English

jQuery事件处理程序逻辑

[英]jQuery event handler logic

I'm having a problem figuring out where I'm going wrong in my code that is supposed to automatically fill in "railQuantity" for a form I am building. 我在弄清楚应该在我要构建的表单中自动填写“ railQuantity”的代码中出问题的地方时遇到了问题。 Currently my code only "works" when either 6ft or 8ft "fenceHeight" is selected but either way it uses the total = (Math.ceil(footage / 8) * 3); 目前,我的代码仅在选择6英尺或8英尺的“ fenceHeight”时才“有效”,但是无论哪种方式它都使用total =(Math.ceil(footage / 8)* 3); equation. 方程。 It will, however, correctly blank out when I choose "Select Fence Height". 但是,当我选择“选择栅栏高度”时,它将正确地消失。 So I am trying to figure out where I am going wrong with my logic so that it does not correctly use the equation for the corresponding fenceHeight. 因此,我试图找出我的逻辑哪里出了问题,以便它不能正确地将方程式用于相应的fenceHeight。 Any and all help is appreciated! 任何和所有帮助表示赞赏! Thanks you guys! 谢谢你们!

Html snippet: HTML片段:

Fence Description: 围栏说明:

<select name="fenceHeight" id="fenceHeight">
    <option value="select">Select Fence Height</option>
    <option value="4" id="fH4">4 Ft.</option>
    <option value="6" id="fH6">6 Ft.</option>
    <option value="8" id="fH8">8 Ft.</option>
</select>

Javascript snippet: JavaScript片段:

//Quantity for Rails
$('#fenceHeight, #footage').bind('keypress keydown keyup change', function() {
    var footage = parseFloat($(':input[name="footage"]').val(),10);
    var total = '';
    if($(':input[name="fenceHeight"]').val() != "select"){
        if(parseFloat($(':input[name="fenceHeight"]').val() == "8")) {
            total = (Math.ceil(footage / 8)* 4);
        } 
        if(parseFloat($(':input[name="fenceHeight"]').val() == "4" || "6")) {
            total = (Math.ceil(footage / 8) * 3);
        } 
        $(':input[name="railQuantity"]').val(total.toString());
    } else {
        $(':input[name="railQuantity"]').val('');
    }
});

Try 尝试

var $footage = $('#footage'),
    $fenceHeight = $('#fenceHeight'),
    $railQuantity = $('input[name="railQuantity"]');
$footage.add($fenceHeight).bind('keypress keydown keyup change', function () {
    var footage = parseFloat($footage.val(), 10),
        fenceHeight = $fenceHeight.val();
    var total = '';
    if (fenceHeight != NaN) {
        if (fenceHeight == '8') {
            total = (Math.ceil(footage / 8) * 4);
        }
        if (fenceHeight == '4' || fenceHeight == '6') {
            total = (Math.ceil(footage / 8) * 3);
        }
        $railQuantity.val(total);
    } else {
        $railQuantity.val('');
    }
});

Demo: Fiddle 演示: 小提琴

These lines are wrong: 这些行是错误的:

    if(parseFloat($(':input[name="fenceHeight"]').val() == "8")) {
        total = (Math.ceil(footage / 8)* 4);
    } 
    if(parseFloat($(':input[name="fenceHeight"]').val() == "4" || "6")) {
        total = (Math.ceil(footage / 8) * 3);
    } 

You're not comparing the result of parseFloat with the strings, you're comparing the val() with the strings and passing the result of the comparison to parseFloat. 您没有将parseFloat的结果与字符串进行比较,而是将val()与字符串进行了比较,并将比较的结果传递给parseFloat。 And in the second one, you're comparing val() with the result of 4 || 6 在第二个中,您将val()4 || 6的结果进行比较 4 || 6 , which is 4 -- you can't combine comparisons with or in programming languages like you can in English. 4 || 6 ,即4 ,您无法像使用英语一样将比较与or以编程语言进行组合。 Change it to: 更改为:

    var height = $(':input[name="fenceHeight"]').val();
    if (height == "8") {
        total = (Math.ceil(footage / 8)* 4);
    } else if (height == "4" || height == "6") {
        total = (Math.ceil(footage / 8) * 3);
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM