【问题标题】:How to get value from dialog in jquery ui?如何从 jquery ui 中的对话框中获取值?
【发布时间】:2015-09-08 04:20:06
【问题描述】:

我已经引用了https://jqueryui.com/dialog/#modal-form 来创建对话框模式表单。

该代码具有向表中插入新行的功能,如下所示:

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

我不明白:提交后,我查看了页面源,没有插入新行。

然后,我在Inspector(Firefox - Ctrl + Shift + C) 中检查了它。我看到了。已插入新行。

我可以"un-hidden"它并显示在源页面上吗?

更新

      <script>
  $(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" );
    });
  });
  </script>

【问题讨论】:

  • 你的有效变量返回什么?
  • @Mohit 它返回true (var valid = true;)。我不是代码的作者。我只参考网站。
  • 好的。你能检查一下 上应用了什么 css 样式吗
  • @Mohit div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; } div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }&lt;tr&gt; css 已定义
  • 如果你将鼠标悬停在 dom 上,你能看到页面上的东西吗?还要检查你的 dom 结构吗?

标签: javascript jquery jquery-ui


【解决方案1】:

“页面源”只显示基于原始文件预加载的代码。

而“检查元素”显示与 DOM 保持同步的代码,并且所有代码都会即时更新。

参考Is the HTML is View Source different from the HTML in Inspect Element?

【讨论】:

    【解决方案2】:

    源页面仅显示您正在查看的文件的内容。由于 JavaScript 不会编辑文件本身,因此无法在源页面中看到这些更改,但这就是检查器的用途。

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签