【问题标题】:How to submit form after validating data not working?验证数据无效后如何提交表单?
【发布时间】:2016-10-27 20:48:14
【问题描述】:

我正在尝试打开一个包含表单的对话框,并且我希望能够检查此表单上的数据。验证数据后,我想提交它。我没有在本地下载 php,所以我添加了警告框,但是当我提交它时没有任何反应。对话框没有关闭。

我是新手,所以据我了解,当我查询$('#dialog-form form') 时,我应该能够应用提交功能。

编辑:将表单操作更改为 action="javascript:alert('Success!')"。我认为问题出在 jquery $("#dialog-form form").submit(); 中。表单不提交,提交有效数据后不允许关闭。

function addUser() {
  var valid=true;
  if ( valid ) {
			$("#dialog-form form").submit(); 
			$(this).dialog("close");
      		}
      		return valid;
}
dialog=$("#dialog-form").dialog({
		autoOpen: false,
		closeOnEscape: true,
		height: 500,
		width: 400,
		title: "title",
		modal:true,
		open: function() {
        		$('.ui-widget-overlay').addClass('custom-overlay');
    		},
		buttons: {
			"Sign up": addUser,
			Cancel: function(){
				dialog.dialog("close");
			}
		},
		close: function() {
			form[ 0 ].reset();
        	allFields.removeClass( "ui-state-error" );
			tips.removeClass( "ui-state-highlight" );
			tips.text("text.");
		},
		show: { effect: "drop", direction: "up", duration: 800 }
		
	});
	form = dialog.find( "form" ).on( "submit", function( event ) {
      		event.preventDefault();
      		addUser();
    	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dialog-form">
			<p class=""></p>

			<form action="javascript:alert('Success!')">
				<fieldset>
					<input type="text" id="fname">
					<input type="text" id="lname">
					<input type="text" id="eid">
					<input type="email" id="email">
					<input type="password" id="pwd">
					<input type="password" id="conf-pwd">
				</fieldset>
			</form>
		</div>

【问题讨论】:

标签: javascript jquery html forms


【解决方案1】:

action= 将 URL 作为其值,而不是纯 JavaScript。尝试将其更改为:

<form action="javascript:alert('Success!')">

【讨论】:

  • 那没有帮助。验证数据后,我希望提交该表单。查询语句似乎不起作用。
【解决方案2】:

我在玩弄代码后更新了我的答案。试试这个:

function addUser() {
  var valid=true;
  if ( valid ) {
            $("#dialog-form form").submit(); 

            }

}
dialog=$("#dialog-form").dialog({
        autoOpen: true,
        closeOnEscape: true,
        height: 500,
        width: 400,
        title: "title",
        modal:true,
        open: function() {
                $('.ui-widget-overlay').addClass('custom-overlay');
            },
       buttons: [{ 
          text: "Sign Up", 
          click: function() { 
            addUser();
            $(this).dialog("close");
          }
       }],
        close: function() {
            form[ 0 ].reset();
            allFields.removeClass( "ui-state-error" );
            tips.removeClass( "ui-state-highlight" );
            tips.text("text.");
        },
        show: { effect: "drop", direction: "up", duration: 800 }

    });

【讨论】:

  • 我改变了它。我认为问题出在 jquery 语句和函数 submit()
  • 不起作用我打开了开发者窗口,当我点击注册或者它抛出“最大调用堆栈大小超过”
  • 检查您的代码并确保只有一个提交调用
  • 或者尝试删除 form = dialog.find( "form" ).on( "submit", function( event ) { event.preventDefault(); addUser(); });
  • 我认为您的代码可能多次调用 addUser()
猜你喜欢
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多