UDPATE
包括Jquery
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.8.21/jquery-ui.min.js"></script>
这是我在这篇文章和另一篇文章之间尝试的整个脚本。
<script type="text/javascript">
/** function remove_entry() {
var answer = confirm("Delete this entry?")
if (answer){
alert('yo');
var identity = $(".red").attr('id');
alert(identity);
}
else{
//do nothing
}
}
$(function() {
$('.red').on('click', '.red', function() {
var eId = this.id.replace(/^\D+/, '');//since IDs should not start with a number
$.post(
'delete.php',
{
id: eId
},
function(data) {
if (data.ok) {//sending JSON responses are easier to debug and you can add to them later without breaking things
//remove row
}
else {
//display error message
}
}
);
});
});
$(function() {
$(".red").click(function() { alert(this.id); });
});**/
/**$(document).on('.red',click(
function(){
alert(this.id);
})
);
$(function() {
$(document).on('.red', click(function() {
alert('yo');
alert($(this).attr('id'));
});
});
/**
$(function() {
$(document).on('.red', 'click',function() {
alert($(this).attr('id'));
});
});**/
$(document).ready(function() {
$('body').on('click', '.red', function() {
alert($(this).attr(id));
});
});
</script>
这是php部分
<?php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR.'dbconnect.php');
$link = new mysqli("$servername", "$username", "$password", "$dbname");
$query = "SELECT COUNT(*) FROM entries";
if ($result = $link->query($query)) {
/* fetch object array */
while ($row = $result->fetch_row()) {
if($row[0]==0){
echo "There are no entries.";
}else {
$query2 = "SELECT id,saying,date,thumbs_up,comments FROM entries ORDER by ID ASC ";
if (($result = $link->query($query2))) {
/* fetch object array */
while ($row = $result->fetch_row()) {
echo
'<div class="container" align="center"">'.
'<div class="entry-container" align="left">'.
$row[1]." ".
'</div>'.
'<div class="x" align="center">'.
'<button class="red" name="remove" id="'.$row[0].'">x'.
'</button>'.
'</div>'.
'</div>'.
'<br>'
;
}
}
}
}
/* free result set */
$result->close();
}
?>
OLDER POST
这是脚本部分
$(function() {
$(".red").click(function() { alert(this.id); });
});
我从数据库输出行,每行都有一个删除按钮,通过单击删除按钮,我需要获取行的id,以便将其发送到删除脚本以删除该特定行。 / p>
这是一个按钮
'<button class="red" name="remove" id="'.$row[0].'">x'.'</button>'.
根据您所看到的内容,为什么我无法提醒所选的ID?
这是错的吗?
$(document).on('.red',click(
function(){
alert(this.id);
})
);
答案 0 :(得分:1)
使用委托功能
试
$(function() {
$(document).on('click','.red',function() {
alert($(this).attr('id'));
});
});
答案 1 :(得分:0)
这有几个问题。我不确定.on
可以与document
一起使用吗?使用'body'
。正如泰米尔人指出的那样,你的on
语法搞砸了。
你想要这个:
$(document).ready(function() {
$('body').on('click', '.red', function() {
alert($(this).attr(id));
});
});