键盘功能是不是改变输入框?

时间:2017-04-22 20:35:52

标签: javascript jquery html

我正在尝试创建一个计算一些字段的简单表单,并将总价格计算为单位成本*数量。我正在尝试使用键盘功能在输入的单价变化时自动更改单价,但是当我改变价格时没有任何反应。

HTML

 <!-- Div for quantity -->
                <div class="form-group">
                    <input type="number" class="form-control" id="quantity" name="quantity" placeholder="Quantity" required>
                </div>
                <!-- Div for unit price -->
                <div class="form-group">
                    <input type="number" class="form-control" id="unitPrice" name="unitPrice" placeholder="Unit Price" required>
                </div>
                <!-- div for cost -->
                <div class="form-group">
                    <input type="number" class="form-control" id="cost" name="cost" placeholder="Cost" required>
                </div>

JS

$(document).ready(function () {
    $("#unitPrice").keyup(function () {
        var quantity = +$("#quantity").val();
        var price = +$("#unitPrice").val();
        $("#cost").val(quantity * unitPrice);
    });
});

});

3 个答案:

答案 0 :(得分:2)

这是一个错字。 变化

$("#cost").val(quantity * unitPrice);

$("#cost").val(quantity * price);

答案 1 :(得分:1)

这是一个有效的解决方案。您的变量名称是

  

<强>价

unitPrice

。希望它有所帮助!

 $(document).ready(function () {
        $("#unitPrice").keyup(function () {
            var quantity = +$("#quantity").val();
            var price = +$("#unitPrice").val();
            $("#cost").val(quantity * price);
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<div class="form-group">
    <input type="number" class="form-control" id="quantity" name="quantity" placeholder="Quantity" required>
</div>
<!-- Div for unit price -->
<div class="form-group">
    <input type="number" class="form-control" id="unitPrice" name="unitPrice" placeholder="Unit Price" required>
</div>
<!-- div for cost -->
<div class="form-group">
    <input type="number" class="form-control" id="cost" name="cost" placeholder="Cost" required>
</div>

答案 2 :(得分:0)

您使用了错误的变量名称。替换此行

# environment variables
top_root=$PWD
src_root=${top_root}/src
patch_root=${top_root}/patches
dist_root=${top_root}/libs/openssl
dist_bin_root=${dist_root}/bin
dist_include_root=${dist_root}/include
dist_lib_root=${dist_root}/lib
build_log=${top_root}/openssl_build.log

# create our folder structure
cd ${top_root}
test -d ${src_root} || mkdir -p ${src_root}
test -d ${dist_root} || mkdir -p ${dist_root}
test -d ${dist_bin_root} || mkdir -p ${dist_bin_root}
test -d ${dist_include_root} || mkdir -p ${dist_include_root}
test -d ${dist_lib_root} || mkdir -p ${dist_lib_root}
touch ${build_log}

rm -f ${build_log}

echo "Building openssl-android ..."

test -d ${src_root}/openssl-android || \
    git clone https://github.com/guardianproject/openssl-android.git ${src_root}/openssl-android >> ${build_log} 2>&1 || \
    die "Couldn't clone openssl-android repository!"
cd ${src_root}/openssl-android

${NDK}/ndk-build >> ${build_log} 2>&1 || die "Couldn't build openssl-android!"

# copy the versioned libraries
#cp -r ${src_root}/openssl-android/libs/armeabi/lib*.so --parents ${dist_lib_root}/.
rsync -a --include '*/' --include '*.so' --exclude '*' ${src_root}/openssl-android/libs/ ${dist_lib_root}/
# copy the executables
#cp -r ${src_root}/openssl-android/libs/armeabi/openssl ${dist_bin_root}/. # work only for one abi folder
rsync -a --include '*/openssl' --exclude '*.so' ${src_root}/openssl-android/libs/ ${dist_bin_root}/
#cp -r ${src_root}/openssl-android/libs/armeabi/ssltest ${dist_bin_root}/. # work only for one abi folder
rsync -a --include '*/ssltest' --exclude '*.so' ${src_root}/openssl-android/libs/ ${dist_bin_root}/
# copy the headers
cp -r ${src_root}/openssl-android/include/* ${dist_include_root}/.

cd ${top_root}

$("#cost").val(quantity * unitPrice);