Dialog在If语句中不起作用

时间:2021-01-01 20:20:14

Hi I have an alert which shows when my form has been saved.

嗨,我有一个警报,显示我的表格保存的时间。

However when I add a dialog it does not work for some reason..

但是,当我添加一个对话框时,由于某种原因它不起作用..

I am using Html.BeginForm and calling onsubmit = "validateForm(event)" as seen below:

我正在使用Html.BeginForm并调用onsubmit =“validateForm(event)”,如下所示:

        @using (Html.BeginForm("Save", "ReadingsEntry", FormMethod.Post, new { enctype = "multipart/form-data", onsubmit = "validateForm(event)" }))
        {

I have used this alert which works fine when I click submit:

我使用此警报,当我点击提交时工作正常:

    if (validateForm = true) {
        alert("test");
    }

But if I change this to a dialog it does not work:

但是如果我将其更改为对话框则不起作用:

    if (validateForm = true) {
        $("#dialog").dialog({
            modal: true,
            buttons: {
                Ok: function () {
                    $(this).dialog("close");
                }
            }
        });
    }

with the html:

用html:

 <div id="dialog" title="Basic dialog">
<p>Thank you for submitting this form. This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>

Any idea why my alert works but not my Dialog, am assuming its because maybe its not liking it because its in a if statement?

任何想法为什么我的警报工作但不是我的Dialog,我假设它,因为它可能不喜欢它,因为它在if语句中?

I have also tried it on the validateForm function:

我也在validateForm函数上尝试过:

   function validateForm() {
    $("#dialog").dialog({
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
            }
        }
    });
}

Still no luck..

仍然没有运气..

Thanks

1 个解决方案

#1


2  

This is an assignment of true to validateForm:

这是对validateForm的true赋值:

if (validateForm = true)

So the expression will always be true regardless of what was in validateForm (and it "was" a function)!

因此无论validateForm中有什么(并且它是“函数”),表达式总是为真!

this is a test of the result of calling validateForm being equal to true:

这是对调用validateForm等于true的结果的测试:

if (validateForm() == true)

And this is the preferred way to check a boolean for truthyness:

这是检查truthyness的布尔值的首选方法:

if (validateForm())

Having said all that, your validateForm does not even return a value, so this entire problem needs a rethink. Can you please explain the overall aim of your code? :)

说了这么多,你的validateForm甚至没有返回一个值,所以整个问题需要重新思考。能否解释一下代码的总体目标? :)

Ignoring the additional issues, you can test further by adding a return true; to your function (just for now):

忽略其他问题,您可以通过添加return true进一步测试;到你的功能(只是为了现在):

function validateForm() {
    ...
    return true
}

then this, at least, will now open the dialog:

那么这至少会打开对话框:

if (validateForm()) {
    $("#dialog").dialog({
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
            }
        }
    });
}

#1


2  

This is an assignment of true to validateForm:

这是对validateForm的true赋值:

if (validateForm = true)

So the expression will always be true regardless of what was in validateForm (and it "was" a function)!

因此无论validateForm中有什么(并且它是“函数”),表达式总是为真!

this is a test of the result of calling validateForm being equal to true:

这是对调用validateForm等于true的结果的测试:

if (validateForm() == true)

And this is the preferred way to check a boolean for truthyness:

这是检查truthyness的布尔值的首选方法:

if (validateForm())

Having said all that, your validateForm does not even return a value, so this entire problem needs a rethink. Can you please explain the overall aim of your code? :)

说了这么多,你的validateForm甚至没有返回一个值,所以整个问题需要重新思考。能否解释一下代码的总体目标? :)

Ignoring the additional issues, you can test further by adding a return true; to your function (just for now):

忽略其他问题,您可以通过添加return true进一步测试;到你的功能(只是为了现在):

function validateForm() {
    ...
    return true
}

then this, at least, will now open the dialog:

那么这至少会打开对话框:

if (validateForm()) {
    $("#dialog").dialog({
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
            }
        }
    });
}