如果声明给我未定义的结果

时间:2013-03-22 15:26:54

标签: javascript

我已经构建了一个表单,根据用户选择的内容显示结果。我试图将if语句放在一起,这将在选择特定值时输出数据表1。 我想当有人选择525和134和290给我数据表1其他给我数据表3.这是我的代码:

<form name="test" id="test"">
    <label for="select1">Height in (mm)</label>
        <select name="division-select" id="height">
        <option label="Please Select"></option>
            <option value="525">525 or less</option>
            <option value="645">645 or less</option>
            <option value="1265">up to 1265</option>
            <option value="1270">more than 1265</option>
        </select>
        <label for="select1">Width in (mm)</label>
        <select name="division-select" id="width">
        <option label="Please Select"></option>
            <option value="134w">134 or less</option>
            <option value="190w">190 or less </option>
            <option value="290w">290 or less</option>
            <option value="328w">328 or less</option>
            <option value="420w">420 or more</option>
        </select>
        <Br>
        <br>
        <label for="select1">Depth in (mm)</label>
        <select name="division-select" id="depth">
        <option label="Please Select"></option>
            <option value="134d">134 or less</option>
            <option value="190d">190 or less </option>
            <option value="290d">290 or less</option>
            <option value="328d">328 or less</option>
            <option value="420d">420 or more</option>
        </select>
        <br><br>
        <h2>Or select by load</h2>
        <label for="select1">Load in (kN)</label>
        <select name="division-select" id="load">
            <option label="Please Select"></option>
            <option value="2-5">2.5 or less</option>
            <option value="5">5 or less </option>
            <option value="10">10 or less</option>
            <option value="25">25 or less</option>
            <option value="50">50 or more</option>
        </select>
        <br>
        <p><input type="button" onclick="calculate();" value="Find your Datasheet" /><input type="button" onclick="formReset()" value="Reset form"></p>

</form>
<div id="result">
</div>
<script type="text/javascript">
function calculate() {
    var height = document.getElementById('height').value;
    var width = document.getElementById('width').value;
    var depth = document.getElementById('depth').value;
    var load = document.getElementById('load').value;
    if (document.getElementById("height").value == "") {
        var heightmsg = "Please select your sample's height.";
        document.getElementById("result").innerHTML = heightmsg;
        return false;
    }
    if (document.getElementById("width").value == "") {
        var widthmsg = "Please select your sample's width.";
        document.getElementById("result").innerHTML = widthmsg;
        return false;
    }
    if (document.getElementById("depth").value == "") {
        var depthmsg = "Please select your sample's depth.";
        document.getElementById("result").innerHTML = depthmsg;
        return false;
    }
    if (height === "525" && width === "134" && depth === "290") {
        if (height === "525" && width === "290" && depth === "134") {
            var str = '<a href="#">Download your Datasheet 1</a>';
        } else {
            var str = '<a href="#">Download your Datasheet 3</a>';
        }
    } else if (height == 1270) {
        var str = "This configuration is beyond the standard range of top-load testers. Please contact Mecmesin Sales to discuss ways to meet your requirements.";
    }
    document.getElementById("result").innerHTML = str;
}

function formReset() {
    document.getElementById("test").reset();
}
</script>

提前致谢。

3 个答案:

答案 0 :(得分:3)

您的宽度和深度值以w和d结尾(例如<option value="190d">),但您不会在if条件(if (height === "525" && width === "134" && depth === "290"))中检查该值。从这些值中取出w和d:

<select name="division-select" id="width">
    <option label="Please Select"></option>
    <option value="134">134 or less</option>
    <option value="190">190 or less</option>
    <option value="290">290 or less</option>
    <option value="328">328 or less</option>
    <option value="420">420 or more</option>
</select>

<强> jsFiddle example

旁注:你的条件似乎永远不会成真:

if (height === "525" && width === "134" && depth === "290") {
    if (height === "525" && width === "290" && depth === "134")...

答案 1 :(得分:1)

if (height === "525" && width === "134" && depth === "290") {
    if (height === "525" && width === "290" && depth === "134") {

毫无意义。如果第一个条件为真,则第二个条件永远不可能。我想你想要

if (height === "525" && width === "290" && depth === "134")
    var str = '<a href="#">Download your Datasheet 1</a>';
else if (height === "525" && width === "134" && depth === "290")
    var str = '<a href="#">Download your Datasheet 3</a>';
else if (height == 1270)
    var str = "This configuration is beyond the standard range of top-load testers. Please contact Mecmesin Sales to discuss ways to meet your requirements.";

另请注意,<option>标记的值后缀为wd,因此它们不等于无后缀数字。

答案 2 :(得分:0)

你使用了太多的if语句,如果你想为三个选择输入(宽度,高度,深度)中的每个选项指定一个单独的链接,你最终会得到4x5x5 = 100 if语句。最简单的方法是创建一个包含所有值和通信链接的mysql数据库,或者你也可以使用XML或JSON数组。

您可以使用空选择输入验证:

var height = document.getElementById('height'),
    width = document.getElementById('width'),
    depth = document.getElementById('depth'),
    load = document.getElementById('load'),
    result = document.getElementById('result'),
    arr = [height, width, depth],
    error = "Please select your sample's ";

for (var i = 0; i < arr.length; i++) {
    var t = arr[i].id;
    if (arr[i].value == "") result.innerHTML = error + t;
    return false;
}

对于JSON示例,请参阅jsfiddle上的演示here