代码有效,但第一次点击时,它什么也不做

时间:2021-06-20 16:59:41

标签: javascript jquery

我有这个 jquery 代码

它没有按预期工作

 mport pyspark.sql.functions as F
df = (spark.createDataFrame([[1, '2021-01-01', '2021-01-10'],
                           [1, '2021-01-11', '2021-01-12'],
                           [1, '2021-01-14', '2021-01-16'],
                           [1, '2021-01-17', '2021-01-20'],
                           [2, '2021-01-01', '2021-01-10'],
                           [2, '2021-01-12', '2021-01-14'],
                           [2, '2021-01-14', '2021-01-15'],
                           [2, '2021-01-19', '2021-01-20'],
                            ], schema="ID int, From string, To string")
      .selectExpr('ID',
                  'to_date(From, "yyyy-MM-dd") as StartDate',
                 'to_date(To, "yyyy-MM-dd") as StopDate')
     )

# Do actual calculation
df_result = (df
            # Get all included dates
           .selectExpr("ID", "explode(sequence(StartDate, StopDate)) as dates")
             # Get previous and next date
            .withColumn("Previous", F.expr('LAG(dates) OVER (PARTITION BY ID ORDER BY dates ASC)'))
            .withColumn("Next", F.expr('LAG(dates) OVER (PARTITION BY ID ORDER BY dates DESC)'))
            # Flag beginnings and endings of intervals
            .withColumn("Begin", F.expr("datediff(dates, previous)> 1 OR previous is NULL"))
            .withColumn("End", F.expr("datediff( next, dates)> 1 OR next is NULL"))
            # Only keep beginnings and endings
            .filter("Begin OR End")
            # Get end next to begin and only keep beginnings
            .withColumn("IntervalEnd", F.expr('LAG(dates) OVER (PARTITION BY ID ORDER BY dates DESC)'))
            .filter("Begin")
            # Rename columns + calculate gaps
            .selectExpr(
                       "ID",
                       "dates as StartDate",
                       "IntervalEnd as StopDate",
                       "datediff(dates, LAG(IntervalEnd) OVER (PARTITION BY ID ORDER BY dates ASC)) as gap_from_previous_in_days",
                       "datediff(LAG(dates) OVER (PARTITION BY ID ORDER BY dates DESC), IntervalEnd ) as gap_to_next_in_days"
                       )
           )
df_result.show()

它只适用于第二次点击,但不适用于第一次点击,

$(document).on('submit','#frmNotification',function(e) { var results = checkForm(this); if (results) { $('#frmNotification').validate({ submitHandler: function(form){ $.ajax({ url: form.action, type: form.method, data: $(form).serialize(), success: function(response) { Swal.fire({ type: "info", text: response, confirmButtonClass: "btn btn-confirm mt-2" }); } }); } }); } e.preventDefault(); }); 是表单名

按钮名称是 frmNotification

1 个答案:

答案 0 :(得分:0)

问题是您在第一次提交后附加了实际的提交侦听器...

试试这个:

$('#frmNotification').validate({
    submitHandler: function(form) {
        var results = checkForm(form);
        if (results) {
            $.ajax({
                url: form.action,
                type: form.method,
                data: $(form).serialize(),
                success: function(response) {
                    Swal.fire({
                        type: "info",
                        text: response,
                        confirmButtonClass: "btn btn-confirm mt-2"
                    });
                }
            });
        }
    }
});