我在我的代码中找出错误的地方时遇到了问题,该代码应该自动为我正在构建的表单填写“railQuantity”。目前我的代码仅在选择了6英尺或8英尺“fenceHeight”时“工作”,但无论哪种方式使用总数=(Math.ceil(素材/ 8)* 3);方程。但是,当我选择“选择围栏高度”时,它会正确消隐。所以我试图找出我的逻辑错误,以便它没有正确使用相应的fenceHeight的等式。任何和所有的帮助表示赞赏!谢谢你们!
Html片段:
围栏描述:
<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片段:
//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('');
}
});
答案 0 :(得分:1)
尝试
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('');
}
});
演示:Fiddle
答案 1 :(得分:1)
这些行错了:
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);
}
您没有将parseFloat
的结果与字符串进行比较,您将val()
与字符串进行比较,并将比较结果传递给parseFloat。在第二个中,您将val()
与4 || 6
的结果进行比较,即4
- 您无法在编程语言中将or
与 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);
}
进行比较你可以用英语。将其更改为:
{{1}}