如果比较的值为真,则$ .getJSON() [英] If compared values are true then $.getJSON()

查看:83
本文介绍了如果比较的值为真,则$ .getJSON()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前的

This is a follow up to my earlier question.

我想比较填写表格后得到的结果.

I wanted to compare the results I get back from filling out a form.

这是我到目前为止所拥有的:

This is what I have so far:

const eqObj = (obj, source) =>
    Object.keys(source).every(key => obj.hasOwnProperty(key) && obj[key] === source[key]);

$('#btn').on('click', function() {
    let $inputs = $('#new_form :input');
    let new_vals = {};
    $inputs.each(function() {
        new_form[this.id] = $(this).val();
    });

    console.log(new_vals);
    $.getJSON(api, function(data) {
        data.forEach(d => {
            console.log(d.values);
            if (eqObj(new_vals, d.values){//open first models and append matched values}
            else {//open other modal}
        });
    });
});

我的new_vals的console.log():{start_date: "2019-12-25", end_date: "2020-04-15"}

My console.log() for new_vals: {start_date: "2019-12-25", end_date: "2020-04-15"}

d.values的我的console.log():

my console.log() for d.values:

{start_date: "2020-01-01", end_date: "2020-03-15"}
{start_date: "2020-01-01", end_date: "2020-03-15"}
{start_date: "2019-12-25", end_date: "2020-04-15"}
{start_date: "2020-03-20", end_date: "2020-03-31"}
{start_date: "2019-10-01", end_date: "2020-03-31"}
{start_date: "2019-10-01", end_date: "2020-03-31"}
{start_date: "2020-01-01", end_date: "2020-01-31"}
{start_date: "2020-01-19", end_date: "2020-01-25"}

当我输入不匹配的值时,我可以根据需要打开第二个模态,但是当我在表单中输入匹配的值时,它将同时打开两个模态.

When I enter the unmatched values, I am able to open the second modal as I want, but when I enter the matched value in my form, it opens both modals.

为什么在我的if语句中同时打开两个模式?有什么办法可以在if语句中执行$ .getJSON()吗?我不需要吗如果有匹配项,我只需要$ .getJson(),否则,我需要打开另一个模式.

Why are both modals opening in my if statement? Is there any way I just do the $.getJSON() in my if statement? I don't need it? I only need the $.getJson() if there are matches, if not I need to open another modal.

推荐答案

您可能需要改进if语句. 如果您只想显示一个或另一个模态,而从不显示,则需要在匹配发生后立即返回. 试试这个:

You probably need to improve your if statement. If you want to show one or another modal only and never both you need to return as soon as the match happens. Try this:

       data.forEach(d => {
            console.log(d.values);
            if (eqObj(new_run_vals, d.values) {
              //open first models and append matched values;
              return;
            }
            //open other modal
        });

这篇关于如果比较的值为真,则$ .getJSON()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆