构建一个数组,但无法找到链接代码

时间:2015-11-30 08:40:16

标签: javascript html arrays

好吧所以我已经构建了这个数组,但我希望如此,当他们在“post code”文本框中输入他们的邮政编码时,它会读取数组,如果他们的邮政编码在数组中,则会将2加到金额/“总计”文本框....我怎么会这样做,因为我已经被困了大约两个星期了....?任何帮助表示赞赏!谢谢!

<!DOCTYPE html>
    <html>
    <body>

    <b>Post Code</b><input type="text" myfunction()">
    <p>
    <b>Total: &pound; </b><input name="amount" size=8 value="0">
    </p>
    <p id="demo"></p>

    <script>
    function myFunction() {
        var postcode = ["ZE", "KW","IV","HS","AB","DD","PH","PA","FK","KY","G","BT","IM,"GY","JE","EH","ML","KA"];
        var a = postcode.indexOf("CV","LE");
        document.getElementById("value").innerHTML = a;
    }
    </script>
    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

上面的代码中有一些拼写错误。我假设数组中提到了pincode的前两个字母。以下是我提出的代码。希望它有所帮助..

<!DOCTYPE html>
<html>
<body>

<b>Post Code</b><input name="postcode" type="text" onblur="myFunction(this.value);">
<p>
<b>Total: &pound; </b><input id="amount" name="amount" size=8 value="0">
</p>
<p id="demo"></p>

<script>
function myFunction(pc) {
    var postcode_arr = ["ZE", "KW","IV","HS","AB","DD","PH","PA","FK","KY","G","BT","IM","GY","JE","EH","ML","KA"];

    var firsttwo = pc.substr(0,2);
    firsttwo = firsttwo.toUpperCase();
    //alert(firsttwo);

    var a = postcode_arr.indexOf(firsttwo);
    if(a!= -1) {
        var amt = parseFloat(document.getElementById("amount").value);
        amt +=2;
        document.getElementById("amount").value = amt;
    }

    document.getElementById("demo").innerHTML = a;


}
</script>
</body>
</html>