【发布时间】:2015-07-19 04:53:08
【问题描述】:
我正在创建一个 php 论坛并使用相同的按钮,这样如果单击一个按钮,它就会执行一项功能。无论如何,对于这个问题,我有一个改变形式的我的 php 脚本有同样的问题。如果单击按钮,此脚本应该会提醒消息。两个按钮具有相同的名称,但警报仅适用于第一个按钮而不是第二个按钮。为什么?我该如何解决?
buttons.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
$("document").ready(function() {
$('#oneButton').bind('click', alertButtonClick);
});
function alertButtonClick()
{
alert("There was a button clicked");
}
</script>
</head>
<body>
<?php
echo "<div class='comments'>";
echo "<form id='theForm'>";
echo "<button type='button' id='oneButton'>Post Comment</button></form></div>";
echo "<div class='comments'>";
echo "<form id='theForm'>";
echo "<button type='button' id='oneButton'>Post Comment</button></form></div>";
?>
</body>
</html>
编辑: 当我修复按钮问题时,我很快遇到了另一个问题。我已经尝试解决它很长时间了。无论如何,我在下面发布了两个代码。每个 div 都具有值、文本区域和按钮。当我在第一个 div 中编写文本并单击“发表评论”按钮时,它会播放该文本和 value=1,这就是我想要的两个按钮。但是,如果我对第二个按钮执行相同操作,它仍然会为第一个按钮输出内容,而不是为第二个按钮输出内容,为什么?这是代码,请尝试并帮助我。
button2.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
$("document").ready(function()
{
$('.oneButton').bind('click',sendInfoToServer);
});
function sendInfoToServer()
{
$('span.commentSent').load('button3.php',
$('#theForm').serializeArray());
}
</script>
</head>
<body>
<span class='commentSent'></span>
<?php
echo "<div class='comments'>";
echo "<form id='theForm'>";
echo "<input type=hidden name='mess_block' value=1>";
echo "<textarea name='comment' class='comment' cols=60 rows=2>Enter Comment…</textarea><br />";
echo "<button type='button' class='oneButton'>Post Comment</button></form></div>"; //check the change
echo "<div class='comments'>";
echo "<form id='theForm'>";
echo "<input type=hidden name='mess_block' value=2>";
echo "<textarea name='comment' class='comment' cols=60 rows=2>Enter Comment…</textarea><br />";
echo "<button type='button' class='oneButton'>Post Comment</button></form></div>"; // check the change
?>
</body>
</html>
button3.php
<?php
$mb = $_POST["mess_block"];
echo $mb . "<br/>";
$mt = $_POST["comment"];
echo $mt . "<br/>";
?>
【问题讨论】:
标签: javascript php jquery button jbutton