简体   繁体   English

如何在.NET Core MVC中打开模式弹出窗口而无需单击按钮

[英]How to open modal popup without button click in .net core MVC

I have a custom modal popup that displays a message if the data that was entered in the view does not match the requirements etc an entered medicine does not suit for the person that the medicine was described to. 我有一个自定义模式弹出窗口,如果在视图中输入的数据不符合要求等,那么所输入的药物不适合描述该药物的人,则显示一条消息。 Here is my code for the script: 这是我的脚本代码:

$(document).ready(function () {
    $("#btnShowModal").click(function () {
        $("#loginModal").modal('show');
    });

    $("#btnHideModal").click(function () {
        $("#loginModal").modal('hide');
    });
});

This handles well the instances where I want to open the popup with a button click, but I would like it to be opened when a certain condition is filled, for example: 这很好地处理了我想通过单击按钮打开弹出窗口的情况,但是我希望在满足特定条件时将其打开,例如:

<script type="text/javascript">
    function openPopup() {
        $("#loginModal").modal('show');
    }
    @if (afterError)
    {

        @:openPopup();
    }
</script>

This does not work however, how should I handle this? 但是,这不起作用,我应该如何处理?

Your JavaScript function definition will work just fine, but your function is not being called because of your razor C# code. 您的JavaScript函数定义可以正常工作,但是由于您的剃须刀C#代码未调用您的函数。 If that is meant to be C# code than it's not valid, you have to explicitly check for true & false values. 如果这是无效的C#代码,则必须显式检查true和false值。 In JavaScript your can just simply equate truthy and falsy values the way that you did it in your original post... 在JavaScript中,您可以像在原始帖子中那样简单地将真实和虚假的价值等同起来...

example... 例...

if (someCondition) is valid in JavaScript but not C#. if(someCondition)在JavaScript中有效,但在C#中无效。

<script type="text/javascript">
    function openPopup() {
        $("#loginModal").modal('show');
    }

    @if (afterError != null)
    {

        openPopup();
    }
</script>

Also keep in mind your logic is only called once on page load. 另外请记住,页面加载仅调用一次您的逻辑。 You can fire off that function from any event though... mouseclick, mouseenter etc... 您可以通过任何事件关闭该功能,例如:mouseclick,mouseenter等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM