HTML到jQuery再到PHP,然后从PHP到jQuery再到HTML
好的,我有这个代码:HTML到jQuery再到PHP,然后从PHP到jQuery再到HTML,php,javascript,jquery,Php,Javascript,Jquery,好的,我有这个代码: <html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script type="text/javascript"> function get() { $.post('tt.php', { name : $(this).attr('id') }, fu
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
function get() {
$.post('tt.php', { name : $(this).attr('id') }, function(output) {
$('#age').html(output).show();
});
}
</script>
</head>
<body>
<input type="text" id="name"/>
<a id="grabe" href="javascript: get()">haller</a>
<div id="age">See your name</div>
</body>
</html>
函数get(){
$.post('tt.php',{name:$(this.attr('id')},函数(输出){
$('#age').html(输出).show();
});
}
看到你的名字了吗
我想要的是:从用户通过jQuery单击的锚标记中获取锚标记的id,然后将其传递到PHP文件。PHP文件随后将用户单击的锚定标记的id名称回显到jQuery,jQuery随后将输出显示到指定的元素中
以下是PHP文件:
<?
$name=$_POST['name'];
if ($name==NULL)
{
echo "No text";
}
else
{
echo $name;
}
?>
您可以使用$(this)
访问代码中关于“是什么让您来到这里”的几乎任何内容。您还需要事件的侦听器。在下面这个过于简短的例子中,我让它监听对
跨度的任何单击
<script>
$(document).ready(function(){
$('span.edit_area').click( function(){
var fieldname = $(this).attr('id');
...
});
});
$(文档).ready(函数(){
$('span.edit_area')。单击(函数(){
var fieldname=$(this.attr('id');
...
});
});
我不知道为什么要将id发送到服务器,然后从ajax调用中再次接收该id,因为它已经存在于客户端脚本中。无论如何,这就是代码
此代码将对所有名为“crazyLink
”类的标记执行此功能,并在id为anotherCrazyDiv
的div中显示从服务器页面接收的数据
HTML
请记住,在您的场景中,id变量中的值和数据变量中的值(从ajax调用获得响应后)是相同的。这样绑定事件,因为您使用的是jQuery
$('a').click(function(){
$.post('tt.php', { name : $(this).attr('id') }, function(output) {
$('#age').html(output).show();
});
});
并从href
<a id="grabe" href="#">haller</a>
<a id="kane" href="#">haller 2</a>
您可能希望将javascript与html分开。它使事情变得更容易。它应该看起来像这样 HTML
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"> </script>
<script type="text/javascript" src="javascript/somescript.js"></script>
</head>
<body>
<input type="text" id="name"/>
<a id="grabe" class="someclass">haller</a>
<div id="age">See your name</div>
</body>
</html>
所以id从HTML到JQuery,再到PHP,再回到JQuery?@rcplusplus他只是想让它在这一点上工作。我怀疑这将是他的代码的最终实现。@dqhendricks:是的,这段代码实际上只是一个过期版本,一旦这个过期版本成功,我将在我的项目中实现它,一个cms项目。
$('a').click(function(){
$.post('tt.php', { name : $(this).attr('id') }, function(output) {
$('#age').html(output).show();
});
});
<a id="grabe" href="#">haller</a>
<a id="kane" href="#">haller 2</a>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"> </script>
<script type="text/javascript" src="javascript/somescript.js"></script>
</head>
<body>
<input type="text" id="name"/>
<a id="grabe" class="someclass">haller</a>
<div id="age">See your name</div>
</body>
</html>
$(document).ready(function() {
$('.someclass').click(function() {
$('#age').load(
url: 'tt.php',
data: { name: $(this).attr('id')}
);
});
});