Javascript将点击新网站提醒

时间:2016-11-08 23:51:32

标签: javascript html alert squarespace

这可能是一个我只是没有看到的简单修复,但我正在尝试设置基本的交付区域功能。如果您在交付区域,在提交邮政编码后,警报显示您在我们的区域中,当您单击确定时,它会将您带到预订页面。否则,说他们在该区域之外。我似乎无法获得警报要求进入预订页面的onclick需求。 (P.S.所有javacript进入Squarespace)

<script type="text/javascript">

function IsValidZipCode() {

    var zip = document.getElementById('txtZip').value;
    var ZipArray = ["60543", "60188"];
    var isValid = ZipArray.indexOf(zip); ;
    (isValid );
    if (isValid >= 0){
      alert ('Nice! go ahead and book!')
      // NEED TO LINK TO NEW WEBPAGE
        return true;

    }
    else {
        alert('Sorry, we do not offer delivery here at this time');
        return false;
    }
}
</script>
<form>
Please Enter Zip Code: <input id="txtZip" name="zip" type="number" />
<input onclick="IsValidZipCode()" id="Button" type="submit" value="Submit"/>
</form>

1 个答案:

答案 0 :(得分:0)

从评论中,您的问题似乎是提交按钮在点击时刷新页面。您需要使用e.preventDefault来防止这种情况。

<input onclick="IsValidZipCode(event)" id="Button" type="submit" value="Submit"/>

function IsValidZipCode(event) {
    if (isValid >= 0){
       event.preventDefault;
       alert ('Nice! go ahead and book!')
       window.location.href = 'yoururl.com'
    }
}

这是一个很好的参考:preventDefault inside onclick attribute of a tag

https://jsfiddle.net/yLyvt30y/