javascript - 如何用 jQuery UI 对话框替换 window.alert ('' ) - 合并两个脚本

标签 javascript jquery jquery-ui jquery-ui-dialog

我正在寻求一些关于合并两个 jquery 脚本的帮助。附加的两个示例都具有基本功能,但我没有足够的经验将它们组合成一个脚本。我想要实现的是附加代码 B 中的消息对话框将替换 window.alert(str);来自代码A。

我认为需要重命名函数,但不知道如何复制/采用它。

请在下面找到所附的两个代码示例。

代码A

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />

<link rel="stylesheet" href="css/login.css">
<script src="js/produkty.js" defer></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script>
  $( function() {
    var dialog, form,

      emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
      name = $( "#name" ),
      email = $( "#email" ),
      password = $( "#password" ),
      allFields = $( [] ).add( name ).add( email ).add( password ),
      tips = $( ".validateTips" );

    function updateTips( t ) {
      tips
        .text( t )
        .addClass( "ui-state-highlight" );
      setTimeout(function() {
        tips.removeClass( "ui-state-highlight", 1500 );
      }, 500 );
    }

    function checkLength( o, n, min, max ) {
      if ( o.val().length > max || o.val().length < min ) {
        o.addClass( "ui-state-error" );
        updateTips( "Length of " + n + " must be between " +
          min + " and " + max + "." );
        return false;
      } else {
        return true;
      }
    }

    function checkRegexp( o, regexp, n ) {
      if ( !( regexp.test( o.val() ) ) ) {
        o.addClass( "ui-state-error" );
        updateTips( n );
        return false;
      } else {
        return true;
      }
    }

    function addUser() {
      var valid = true;
      allFields.removeClass( "ui-state-error" );

      valid = valid && checkLength( name, "username", 3, 16 );
      valid = valid && checkLength( email, "email", 6, 80 );
      valid = valid && checkLength( password, "password", 5, 16 );

      valid = valid && checkRegexp( name, /^[a-z]([0-9a-z_\s])+$/i, "Username may consist of a-z, 0-9, underscores, spaces and must begin with a letter." );
      valid = valid && checkRegexp( email, emailRegex, "eg. ui@jquery.com" );
      valid = valid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );

      if ( valid ) {

            $.ajax({
                method: "POST",
                url: "registerEngine.php",
                data: {"name": name.val(), "email": email.val(), "password": password.val()},
             }).done(function( data ) {
                var result = $.parseJSON(data);

                var str = '';
                if(result == 1) {
                        str = 'Nowe konto zarejestrowane prawidłowo.';
                        window.alert(str);
                        window.location.href = "login.php";

                }else if( result == 2) {
                        str == 'Wymagane jest wypełnienie wszystkich pól.';
                        window.alert(str);
                        window.location.href = "register.php";
                } else{
                        str = 'Błąd rejestracji danych. Spróbuj ponownie.';
                        window.alert(str);
                        window.location.href = "register.php";
                }
          });

        dialog.dialog( "close" );

      }
      return valid;
    }

    dialog = $( "#dialog-form" ).dialog({
      autoOpen: true,
      height: 400,
      width: 350,
      modal: true,
      buttons: {
        "Create an account": addUser,
        Cancel: function() {
          dialog.dialog( "close" );
          window.location.href = "index.php";
        }
      },
      close: function() {
        form[ 0 ].reset();
        allFields.removeClass( "ui-state-error" );
      }
    });

    form = dialog.find( "form" ).on( "submit", function( event ) {
      event.preventDefault();
      addUser();
    });

  } );
</script>

<title>Jadłospis - Logowanie</title>
</head>
<body>

<div id="dialog-form" title="Create new user">
  <p class="validateTips">All form fields are required.</p>

  <form>
    <fieldset>
      <label for="name">Name</label>
      <input type="text" name="name" id="name" value="Jane Smith" class="text ui-widget-content ui-corner-all">
      <label for="email">Email</label>
      <input type="text" name="email" id="email" value="jane@smith.com" class="text ui-widget-content ui-corner-all">
      <label for="password">Password</label>
      <input type="password" name="password" id="password" value="xxxxxxx" class="text ui-widget-content ui-corner-all">
      <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
    </fieldset>
  </form>
</div>

</body>
</html>

代码 B - 也是有效的 jquery 对话框,我想将其复制到 Coda A 中。

<html>
<head>
<meta charset="utf-8" />

<title>Usuwanie produktu</title>

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script>
$( function() {
        $( "#dialog-message" ).dialog({
                modal: true,
                buttons: {
                        Ok: function() {
                        $( this ).dialog( "close" );
                        window.location.href = "glowny.php?akcja=produkty";
                        }
                }
        });
        } );
</script>

</head>
<body>

<div id="dialog-message" title="Usuwanie produktu">
        <p>
                <span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
                Wybrany produkt został pomyślnie usunięty z bazy danych.
        </p>
        <p>
                Naciśnij OK aby kontynuować.
        </p>
</div>

</body>
</html>

我对 js/jquery 的经验不超过两天,因此非常欢迎任何帮助。

提前致谢...

最佳答案

我会考虑创建您自己的函数而不是 alert()。考虑以下因素:

function alertDiag(c, t){
  if(t == undefined){
    t = "Alert";
  }
  var diag = $("<div>", {
    title: t
  }).html("<p>" + c + "</p>").dialog({
    modal: true,
    buttons: {
      Ok: function() {
        window.location.href = "glowny.php?akcja=produkty";
      }
    }
  });
}

然后,您可以将 window.alert(str) 替换为 alertDiag(str)。这应该创建并启动对话框。由于您要更改位置并加载新页面,因此不需要关闭对话框。

alertDiag() 将接受 c 作为 HTML 内容。 (可选)它会接受 t 作为标题。如果您想使用特定的标题,则可以按如下方式完成:

alert(str, "Usuwanie produktu");

如果没有定义t,它只会显示“Alert”。您可以将其更改为您首选的默认值。

更新

您还可以增加功能能力。考虑以下因素:

function goto(url){
  if(url === undefined || url === false){
    return false;
  }
  window.location.href = url;
}

function alertDiag(c, t, u){
  if(t === undefined){
    t = "Alert";
  }
  if(u === undefined){
    u = false;
  }
  var diag = $("<div>", {
    title: t
  }).html("<p>" + c + "</p>").dialog({
    modal: true,
    buttons: {
      Ok: function(){
        goto(u);
      }
    }
  });
}

然后使用类似:

alertDiag(str, title, "glowny.php?akcja=produkty");

示例:https://jsfiddle.net/Twisty/pnowcdrg/

希望这有帮助。

关于javascript - 如何用 jQuery UI 对话框替换 window.alert ('' ) - 合并两个脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54497284/

相关文章:

javascript - HTML 中的多个点击

javascript - 使用 1 个或多个 jQuery promise

javascript - MaterializeCSS - 下拉列表未显示在正确的位置

javascript - 如何使所有 Bootstrap 面板均可点击?

javascript - 如何循环遍历数组,每次触发数组中每一项的单击事件

javascript - Jquery UI 自动完成输入 - 如何检查更改时输入是否为空?

jquery - 使用 AJAX 和 CakePHP 保存数据

jQuery UI 可拖动 connectToSortable 不遵守可排序项目选项

javascript - 如何从 ASP.NET 5 WebAPI 调用 javascript 页面上的函数

javascript - 防止递归函数运行 setTimeout 复合