0

我整理了一个简化的问题示例。我的 HTML 代码如下:

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script style="text/javascript">
$(document).ready(function(){

$('a.mylink').click(function(e){
//get the querystring
var href=$(this).attr('href');
var querystring=href.slice(href.indexOf('?')+1);
$.get('test_jq.php', querystring, function(data) {
//load the data
$('#mydiv').html(data);
}); 
//stop the link
return false;
});


$('#contact').submit(function() {
alert("you have submitted");
return false;
}); // end submit


});
</script>
</head>

<body>

<a class="mylink" href="test.php?info=A" >A</a>
<a class="mylink" href="test.php?info=B" >B</a>
<a class="mylink" href="test.php?info=C" >C</a>

<div id="mydiv">
Information loaded here.
</div>
</body>

</html>

这是 test.jq php 文件:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'GET' and isset($_GET["info"]) ) 
   {
   $info=$_GET["info"];

   if($info=="A")echo "A";
   elseif($info=="B")
   {
   echo '<form id="contact" method="post" action="email.php" >
        <label>Name</label>
        <input type="text">
        <button type="submit">Submit</button>
        </form>';
   }
   elseif($info=="C") echo "C";
   }
Here is a working example:

http://www.scratchprogramming.org/test.php

本质上,我无法访问处理程序:

$('#contact').submit(function() {
    alert("you have submitted");
    return false;
    }); // end submit

当我单击 B 链接然后提交表单时?

这是一个工作示例:

工作示例

谢谢..吉利安

4

1 回答 1

1

我找到了解决方案,我使用 .on() 方法将提交处理程序委托给文档。

$(document).on('submit', '#contact', function() {
alert("you have submitted");
return false;
});

这个解决方案是通过 jquery.org 提供给我的

于 2013-08-01T03:31:59.560 回答