如何在jQuery中实现基于不同字段条件的URL跳转?

想实现如下逻辑

  1. 当name字段为空时,判断input输入的值与zhname是否相同,相同跳转到对应的url;
  2. 当zhname字段为空时,判断input输入的值与name是否相同,相同跳转到对应的url;
  3. 如果都为空,不执行跳转链接;
  4. 如果都有数据,input输入的值满足其中一个即可跳转到对应的url。
    代码如下
<script>
    var users = [
        { name: "John", zhname: "约翰", url: "1.html" }
        { name: "Lili", zhname: "", url: "2.html" }
        { name: "", zhname: "张三", url: "3.html" }
    ];
    function check(){
        var username = $("#username").val();
        for (var i = 0; i < users.length; i++) {
            if (/*不会写了*/) {
                window.location.href = users[i].url;
                return;
            }
        }
        alert("Your name entered are incorrect!");
    }
</script>

if这部分按照下边写法,只能满足第四种情况

if (users[i].name === username || users[i].zhname === username) {
    window.location.href = users[i].url;
    return;
}

求大佬指点

阅读 1.2k
3 个回答

1、2、4这几条规则合并下就是判断username先等于name再等于zhname,有一个相等都命中。还要排除掉username为空的情况

function check(){
    var username = $("#username").val();
    if(!username)return
    let target = users.find(item => username === item.name || username === item.zhname)
    if(target){
        return window.location.href = target.url;
    }
    alert("Your name entered are incorrect!");
}
<script>
    var users = [
        { name: "John", zhname: "约翰", url: "1.html" },
        { name: "Lili", zhname: "", url: "2.html" },
        { name: "", zhname: "张三", url: "3.html" }
    ];
    function check(){
        var username = $("#username").val();
        for (var i = 0; i < users.length; i++) {
            if (users[i].name === "" && users[i].zhname !== "") {
                if (users[i].zhname === username) {
                    window.location.href = users[i].url;
                    return;
                }
            } else if (users[i].zhname === "" && users[i].name !== "") {
                if (users[i].name === username) {
                        window.location.href = users[i].url;
                        return;
                    }
             } else if (users[i].name !== "" && users[i].zhname !== "") {
                if (users[i].name === username || users[i].zhname === username) {
                    window.location.href = users[i].url;
                    return;
                }
            }
        }
        alert("Your name entered are incorrect!");
    }
</script>
<script>
    var users = [
        { name: "John", zhname: "约翰", url: "1.html" },
        { name: "Lili", zhname: "", url: "2.html" },
        { name: "", zhname: "张三", url: "3.html" }
    ];

    function check() {
        var username = $("#username").val().trim();
        for (var i = 0; i < users.length; i++) {
            if ((users[i].name === "" && users[i].zhname === username) ||
                (users[i].zhname === "" && users[i].name === username) ||
                (users[i].name === username || users[i].zhname === username)) {
                window.location.href = users[i].url;
                return;
            }
        }
        alert("Your name entered is incorrect!");
    }
</script>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏