简体   繁体   English

Facebox上的Ajax

[英]Ajax on facebox

I have a from in facebox modal ( http://defunkt.io/facebox/ ) which it has an IDand I have a problem calling the selector/ID of the form. 我在facebox模态中有一个from( http://defunkt.io/facebox/ ),它有一个ID,并且我在调用表单的选择器/ ID时遇到问题。

Here are the code 这是代码

=== index.php === === index.php ===

<!DOCTYPE html>
<html>
      <head>
            <script src="jquery.js" type="text/javascript"></script>
            <link href="/facebox/facebox.css" media="screen" rel="stylesheet" type="text/css"/>
            <script src="/facebox/facebox.js" type="text/javascript"></script>
            $(document).ready(function() {
                  $('a[rel*=facebox]').facebox({
                        loadingImage : 'loading.gif',
                        closeImage   : 'closelabel.png'
                  });
                  $('#myform').submit(function(event) {
                        $.ajax({
                              type  : 'POST',
                              url   : 'process.php';
                              data  : {'name' : $('input[name=name]').val(),'email' : $('input[name=email]').val()},
                              success:function() {
                                   alert('success!');
                              },
                        });
                        event.preventDefault();
                  });
            }); 
      </head>
      <body>
            <a href="showform.php" rel="facebox">Show Form</a>
      </body>
</html>

==== showform.php ==== ==== showform.php ====

<form action="process.php" id="myform" method="post" accept-charset="utf-8">
<input type="text" name="name" placeholder="Your Name" >
<input type="email" name="email" placeholder="Your Email" >
</form>

Any help on that is very appreciated! 任何帮助都非常感谢! Thanks 谢谢

You're trying to call an ID located in another page you cant do that your script interact only with the DOM loaded into your page , you need to move your from into index.php , also it should be url:'process.php', , not url : 'process.php'; 您试图调用位于另一个页面中的ID,但是您不能使脚本仅与加载到页面中的DOM交互,您需要将其从移动到index.php,它也应该是url:'process.php',而不是url : 'process.php'; , one more thing , you have no submit botton , here is a working example ,还有一件事,您没有提交botton,这是一个有效的示例

 $('#myform').submit(function(event) { console.log("form submited") $.ajax({ type : 'POST', url : 'process.php', data : {'name' : $('input[name=name]').val(), 'email' : $('input[name=email]').val() }, success:function() { alert('success!'); }, }); event.preventDefault(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="process.php" id="myform" method="post" accept-charset="utf-8"> <input type="text" name="name" placeholder="Your Name" > <input type="email" name="email" placeholder="Your Email" > <input type="submit" value="submit" /> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM