简体   繁体   English

IF语句中的不同window.onload事件

[英]Different window.onload events in IF statement

I'm trying to have different onload events for different options. 我试图为不同的选项提供不同的onload事件。 For example:If my variable $var==1 it triggers one window.onload event,and if the variable $var==2,then the other window.onload event tiggers. 例如:如果我的变量$ var == 1则触发一个window.onload事件,如果变量$ var == 2,则另一个tiggers触发。 But my code doesn't work.I'm taking the variable value from a hidden input with id='alert' Is there another way to do this kind of coding,and what I'm doing wrong here? 但是我的代码不起作用。我从id='alert'的隐藏输入中获取变量值。还有另一种方法可以进行这种编码,而我在这里做错了什么?
Thx guys 谢谢你们

Here is my code 这是我的代码

JavaScript : JavaScript:

<script type="text/javascript">

            function Onload(){

            var variable=document.getElementById("alert").value;

            if(variable.value=="1"){

                window.onload = function()
                {
                    swal({
                    title: "Example",
                    text: "Example123!",
                    type: "success",
                    showCancelButton: false,
                    confirmButtonClass: 'btn-success',
                    confirmButtonText: 'Close'
                    }); 
                };

            }
            if(variable.value=="2"){
                window.onload = function()
                {
                    swal({
                    title: "Example",
                    text: "Example1234",
                    type: "error",
                    showCancelButton: false,
                    confirmButtonClass: 'btn-danger',
                    confirmButtonText: 'Close'
                    }); 
                };
            }
            if(variable.value=="3"){

                window.onload==function(){

                    false;

                }

            }

            }       

</script>

HTML 的HTML

<body onLoad='Onload();'>   
<input type='hidden' id='alert' name='alert' value="<?php echo $alert;  ?>">

There's no need to use hidden input when you can put your variable right into <script> section with json_encode . 当您可以使用json_encode将变量直接放入<script>部分时,就无需使用隐藏输入。 For example: 例如:

<script type="text/javascript">
(function() { // grouping the code into IIFE to prevent global scope pollution
  var modalIndex = <?php echo json_encode($alert); ?>;
  var modalOpts = {
    1: {
      title: 'Example1',
      text: 'Example1234',
      type: 'success',
      showCancelButton: false,
      confirmButtonClass: 'btn-success',
      confirmButtonText: 'Close'
    }, 
    2: {
      title: 'Example2',
      text: 'Example2341',
      type: 'warning',
      showCancelButton: false,
      confirmButtonClass: 'btn-danger',
      confirmButtonText: 'Close'
    }, 
  };
  if (modalIndex in modalOpts) {
    window.onload = function() {
      swal(modalOpts[modalIndex]); 
    } 
  }
})();

Note that I 1) removed Onload function, so that the code will be executed immediately; 请注意,我1)删除了Onload函数,以便代码将立即执行; 2) grouped options into a single object; 2)将选项分组为一个对象; 3) made the transient check for existing of the configuration section set by server. 3)对服务器设置的配置部分是否存在进行瞬时检查。

window.onload is triggering your Onload function. window.onload正在触发您的Onload函数。 Assigning it again within that function will not re-trigger it. 在该函数中再次分配它不会重新触发它。 Instead, just call those functions directly: 而是直接调用这些函数:

function Onload() {
    var variable = document.getElementById("alert").value;
    if (variable == "1") {

        swal({
            title: "Example",
            text: "Example123!",
            type: "success",
            showCancelButton: false,
            confirmButtonClass: 'btn-success',
            confirmButtonText: 'Close'
        });
    }
    if (variable == "2") {

        swal({
            title: "Example",
            text: "Example1234",
            type: "error",
            showCancelButton: false,
            confirmButtonClass: 'btn-danger',
            confirmButtonText: 'Close'
        });

    }
    if (variable == "3") {
        //just do nothing
    }

}

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

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