如何用jQuery UI对话框替换window.alert('')-合并两个脚本 [英] How to replace window.alert('') with jQuery UI dialog - merging two scripts

查看:70
本文介绍了如何用jQuery UI对话框替换window.alert('')-合并两个脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for some help with merging two jquery scripts. Both attached examples are working with basic functionality, but I do not have enough experience in order to combine them into one script. What I would like to achieve is that a message dialog from attached code B would replace window.alert(str); from code A.

我认为将需要重命名功能,但不知道如何复制/采用它.

I think that function renaming will be required, but have no idea how to copy/adopt it.

请在下面找到随附的两个代码示例.

Please find attached both codes examples bellow.

代码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-我也想将其复制到Coda A的同时工作的jquery对话框.

Code B - also working jquery dialog that I would like to copy into 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方面的经验不超过2天,因此非常欢迎您提供帮助.

I have no more than two days experience with js/jquery so any help is most welcome.

预先感谢...

推荐答案

我会考虑创建您自己的函数,而不是alert().请考虑以下内容:

I would consider creating your own function instead of alert(). Consider the following:

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).这将创建并启动对话框.由于您要更改位置并加载新页面,因此无需关闭对话框.

You can then replace window.alert(str) with alertDiag(str). This should create and launch the dialog. Since you're changing the location and loading a new page, closing the dialog is not needed.

alertDiag()将接受c作为HTML内容. (可选)它将接受t作为标题.如果您要使用特定的标题,可以这样进行:

alertDiag() will accept c as the HTML Content. Optionally, it will accept t as the Title. If you have a specific title you want to use, it is done like so:

alert(str, "Usuwanie produktu");

如果未定义t,它将仅说"Alert".您可以将其更改为首选默认值.

If no t is defined, it will simply say "Alert". You can change this to your preferred default.

更新

您还可以增加功能.请考虑以下内容:

You can also increase the functions ability. Consider the following:

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/

希望这会有所帮助.

这篇关于如何用jQuery UI对话框替换window.alert('')-合并两个脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆