0

我已经建立了一个表单,根据用户的选择显示结果。我正在尝试将 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>

提前致谢。

4

3 回答 3

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 示例

旁注:您的情况似乎永远不可能为真:

if (height === "525" && width === "134" && depth === "290") {
    if (height === "525" && width === "290" && depth === "134")...
于 2013-03-22T15:34:05.760 回答
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>标签值以wand为后缀d,因此它们不等于无后缀的数字。

于 2013-03-22T15:37:10.360 回答
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 上的演示

于 2013-03-22T19:46:18.550 回答