javascript - Jquery 模态/对话框表单不起作用

标签 javascript jquery jquery-ui jquery-plugins

嗨,我是编码新手,正在尝试使用 Jquery 库对话框/模态表单。我已准备好所有 CSS、JS 和 HTML,但是当我单击“创建新用户”时,对话框/模式不会出现(按钮后面的功能不起作用)。我在控制台中收到以下错误 - 任何帮助将不胜感激:

控制台中的错误消息:

Uncaught TypeError: undefined is not a function    main.js
and it points to this ---> dialog = $( "#dialog-form" ).dialog({

main.js 文件

$(function() {
    var dialog, form,

    // From http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29
    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 ) {
      $( "#users tbody" ).append( "<tr>" +
        "<td>" + name.val() + "</td>" +
        "<td>" + email.val() + "</td>" +
        "<td>" + password.val() + "</td>" +
      "</tr>" );
      dialog.dialog( "close" );
    }
    return valid;
  }

  dialog = $( "#dialog-form" ).dialog({
    autoOpen: false,
    height: 300,
    width: 350,
    modal: true,
    buttons: {
      "Create an account": addUser,
      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();
  });

  $( "#create-user" ).button().on( "click", function() {
    dialog.dialog( "open" );
  });
});

index.html 文件

<html>
<head>
  <title>Modal form</title>
  <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" />
  <link rel="stylesheet" type="text/css" media="all" href="css/grid.css" />  
  <link rel="stylesheet" type="text/css" media="all" href="css/style.css" /> 
  <link href='http://fonts.googleapis.com/css?family=Lato:100' rel='stylesheet' type='text/css'>
  <script src="js/jquery-1.11.2.js"></script>
  <script src="js/main.js"></script>
</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">
        <!-- Allow form submission with keyboard without duplicating the dialog button -->
        <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
      </fieldset>
    </form>
  </div>

  <div id="users-contain" class="ui-widget">
    <h1>Existing Users:</h1>
    <table id="users" class="ui-widget ui-widget-content">
      <thead>
        <tr class="ui-widget-header ">
          <th>Name</th>
          <th>Email</th>
          <th>Password</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>John Doe</td>
          <td>john.doe@example.com</td>
          <td>johndoe1</td>
        </tr>
      </tbody>
    </table>
  </div>
  <button id="create-user">Create new user</button>

</body>
</html>

最佳答案

确保你有 jquery ui 和与 ui 配套的 css。 你的代码很好,只需将其添加到你的 <head> 中:

<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>

查看工作演示:http://jsfiddle.net/swm53ran/221/

关于javascript - Jquery 模态/对话框表单不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28441397/

相关文章:

c# - 在计时器上刷新 Gridview 但保持扩展 div 的状态

javascript - jQuery 可拖动停止 : function to stopPropagation of elements

javascript - jQuery 在 appendTo 之前查找并替换 <strong> 标签

jquery-ui - 使用 freebase 和 jsonp 自动完成

javascript - 根据在 javascript 或 jquery 中选择的日期禁用日期选择器的先前日期

javascript - 在 D3 中换行长文本标签,无需额外换行

javascript - ng-repeat 内的指令不起作用

javascript - Set.prototype.has() 带谓词

javascript - 以编程方式清除输入文本框

javascript - 如何在 div 最终淡入(JS、jQuery)后更改文本?