循环内的多个对话框功能

问题描述 投票:0回答:1

我尝试从数据库中的某些数据制作多对话框表单,但是当我单击按钮时,对话框表单未显示

我使用PHP增量数字来区分属性ID

然后,我用于将我的php代码中的id与jquery代码中的id同步

当我尝试使用jsfiddle时,它说“在循环之间声明的函数引用外部作用域变量可能会导致语义混乱”

这是我的PHP代码:

<button class="btn btn-danger" id="create-user<?php echo $no2++; ?>">Buka Blokir</button>

    <div id="dialog-form<?php echo $no++; ?>" title="Create new user">
    <p class="validateTips">All form fields are required.</p>

<form>
  <fieldset>
    <label for="reason">Reason</label>
    <input type="text" name="reason" id="reason" value="Jane Smith" class="text ui-widget-content ui-corner-all">

  <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
</fieldset>
</form>
</div>

这是我的jQuery代码:

$( function() {

var dialog, form,

  reason = $( "#reason" ),
  allFields = $( [] ).add( reason ),
  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 unBlock() {
  var valid = true;
  allFields.removeClass( "ui-state-error" );
  valid = valid && checkLength( reason, "reason", 6, 80 );

  if ( valid ) {
    $( "#users tbody" ).append( "<tr>" +
      "<td>" + dasar.val() + "</td>" +
    "</tr>" );
    dialog.dialog( "close" );
  }
  return valid;
}

for (var i = 1; i < 50; i++) {
dialog = $( "#dialog-form"+i ).dialog({
  autoOpen: false,
  height: 400,
  width: 350,
  modal: true,
  buttons: {
    "Unblock": unBlock,
    Cancel: function() {
      dialog.dialog( "close" );
    }
  },
  close: function() {
    form[ 0 ].reset();
    allFields.removeClass( "ui-state-error" );
  }
});
}
form = dialog.find( "form" ).on( "submit", function( event ) {
  event.preventDefault();
  addUser();
});

for (var b = 1; b < 50; b++) {
$( "#create-user"+b ).button().on( "click", function() {
  dialog.dialog( "open" );
});

}

} );
jquery-ui dialog jquery-ui-dialog
1个回答
0
投票

我认为您将其变得过于复杂。考虑以下代码。

$(function() {
  var reasons = $("[id^='reason']"),
    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;
    }
  }

  $("[id^='dialog-form']").dialog({
    autoOpen: false,
    height: 400,
    width: 350,
    modal: true,
    buttons: {
      "Unblock": function() {
        var valid = true;
        reasons.removeClass("ui-state-error");;
        var reason = $(this).find("[id^='reason']");
        valid = valid && checkLength(reason, "reason", 6, 80);
        if (valid) {
          /*
          $("#users tbody").append("<tr>" +
            "<td>" + dasar.val() + "</td>" +
            "</tr>");
          */
          $("[id^='dialog-form']").dialog("close");
        }
        return valid;
      },
      Cancel: function() {
        $(this).dialog("close");
      }
    },
    close: function() {
      $(this).find("form")[0].reset();
      reasons.removeClass("ui-state-error");
    }
  });

  $("div[id^='dialog-form'] form").on("submit", function(e) {
    e.preventDefault();
    /*
    addUser();
    */
  });

  $("button[id^='create-user']").on("click", function(e) {
    var dlg = $("#dialog-form-" + $(this).attr("id").slice(-1));
    dlg.dialog("open");
  });
});
<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>
<button class="btn btn-danger" id="create-user-1">Buka Blokir</button>
<div id="dialog-form-1" title="Create new user">
  <p class="validateTips">All form fields are required.</p>
  <form>
    <fieldset>
      <label for="reason-1">Reason</label>
      <input type="text" name="reason" id="reason-1" value="Jane Smith" class="text ui-widget-content ui-corner-all">
      <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
    </fieldset>
  </form>
</div>

<button class="btn btn-danger" id="create-user-2">Buka Blokir</button>
<div id="dialog-form-2" title="Create new user">
  <p class="validateTips">All form fields are required.</p>
  <form>
    <fieldset>
      <label for="reason-2">Reason</label>
      <input type="text" name="reason" id="reason-2" value="John Smith" class="text ui-widget-content ui-corner-all">
      <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
    </fieldset>
  </form>
</div>

使用一个包含多个元素的选择器,您仍然可以为其分配UI窗口小部件。您只需要了解如何使用this$(this)即可发挥自己的优势。

© www.soinside.com 2019 - 2024. All rights reserved.