Javascript计算

时间:2014-01-25 01:29:43

标签: javascript jquery jquery-selectors

您好我想问一下如何在这里得到答案?我得到了NaN,我试图为每个变量添加parseFloat。另外,如果用户从收音机框中选择“否”,则下一个问题的值将为零。谢谢你的帮助!

<script type="text/javascript">
$(document).ready(function() {
    $("input[name=q-1], input[name=q-2], input[name=q-4], input[name=q-6]").change(function(e) {

        var $row = $(this).parent();
        var cooling_area_sqm = $row.find("input[name=q-1]").val();
        var people = $row.find("input[name=q-2]").val();
        var glass_walls_m = $row.find("input[name=q-4]").val();
        var ceiling_m = $row.find("input[name=q-6]").val();

        var sqm_tr = 18;
        var sqm_tr_hp = 1.25;
        var heat_gain = 0.3;
        var solar_penetration = 0.2;
        var ceiling_height = 1.1;

        var mTons = 0;
        var mHP = 0;
        var mHeatGainHP = 0;
        var mGlassWallsHP = 0;
        var mCeilingHP = 0;
        var temp_var = 0;

            mTons = parseFloat(mTons);
            mHP = parseFloat(mHP);
            mHeatGainHP = parseFloat(mHeatGainHP);
            mGlassWallsHP = parseFloat(mGlassWallsHP);
            mCeilingHP = parseFloat(mCeilingHP);

            mTons = cooling_area_sqm / sqm_tr;
            <!-- start of error -->
                            mHP = mTons * sqm_tr_hp;
            mHeatGainHP = (people * heat_gain) / 10;
            mHP = mHP + (mHP * mHeatGainHP);
            mGlassWallsHP = glass_walls_m * solar_penetration;
            mHP = mHP + mGlassWallsHP;
            mCeilingHP = ceiling_m * ceiling_height;
            mHP = mHP * mCeilingHP;

        $(".capacity").val(mHP);

        <!-- end of error -->
    });
});
</script>
<form method="post">
<ol>
    <li>
        <label>Question?</label><br/>
        <input class="input-text q-input" type="text" name="q-1" value="<?php echo $cooling_area_sqm; ?>"/>&nbsp;sqm
    </li>
    <li>
        <label>Question?</label><br/>
        <input class="input-text q-input" type="text" name="q-2" value="<?php echo $people; ?>"/>&nbsp;people
    </li>
    <li>
        <label>Question?</label><br/>
        <input type="radio" name="q-3" value="yes-3"/>&nbsp;Yes
        <br/>
        <input type="radio" name="q-3" value="no-3"/>&nbsp;No
        <br/>
    </li>
    <li>
        <label>Question?</label><br/>
        <input class="input-text q-input" type="text" name="q-4" value="<?php echo $glass_walls_m; ?>"/>&nbsp;meter/s
    </li>
    <li>
        <label>Question?</label><br/>
        <input type="radio" name="q-5" value="yes-5"/>&nbsp;Yes
        <br/>
        <input type="radio" name="q-5" value="no-5"/>&nbsp;No
    </li>
    <li>
        <label>Question?</label><br/>
        <input class="input-text q-input" type="text" name="q-6" value="<?php echo $ceiling_m; ?>"/>&nbsp;meter/s
        <div class="add-info">
            example: 4 meters + 1 meter more
        </div>
    </li>
</ol>
</form>

<div class="capacity-cont">
<label>Question?</label><br/>
<input class="input-text q-input capacity" type="text" name="c-7" disabled />&nbsp;HP
</div>

http://jsfiddle.net/Uk5bN/

2 个答案:

答案 0 :(得分:1)

我还会改变其他事情,但最低限度的是:

var $row = $(this).closest('form');

http://jsfiddle.net/Uk5bN/1/

当您执行$row = $(this).parent()时,您将获得li,这是一个同行而不是其他li的父级,其中包含您正在尝试的字段到$.find()form高于您尝试选择的所有input

答案 1 :(得分:0)

请记住parseFloat(“”)== NaN。它只需要一个操作数就可以用来搞砸你的计算。