我有这样的代码:
<script>
$(document).ready(function() {
$("#div_trattamenti").on('change', '.inpt_chkbox_trattamenti', function (e) {
e.preventDefault();
if($(this).is(":checked")){
var id = $(this).attr('rel');
var value = $(this).val();
alert(value); // prezzo
}if($(this).is(":not(:checked)")){
var id = $(this).attr('rel');
var value = 0;
alert(value); // prezzo
}
data_chk = 'id=' + id + '&value=' + value;
$.ajax({
type: "POST",
data: data_chk,
cache: false,
url: "php/aggiorna_chiuso.php",
success: function(html){
var obj = $.parseJSON(html);
$.each(obj,function(index, element){
var value2 = element.chiuso; // value chiuso
}); // fine .each
if(value == 1){
$('.chkmk_img_trattamenti', this).show();
}
if(value == 0){
$('.chkmk_img_trattamenti', this).hide();
}
}, // fine success
error: function(xhr, status, error) {
console.log(status);
console.log(error);
console.dir(xhr);
} // fine error
}); // fine ajax
});
});
</script>
它的工作,但我不能显示图像仅为选定的行(当用户单击复选框)。
我试过这个,但它不工作。:
$('。chkmk_img_trattamenti’,这个),告诉();
html代码: $html = '';
$html .= "<table class='class_table'>";
while( $row = mysql_fetch_array($query_1)){
$html .= "<tr class='row'>";
$html .= "<td class='col_1'>";
$html .= "$row[prestazione]";
$html .= "</td>";
$html .= "<td class='col_2'>";
$html .= "$row[denti]";
$html .= "</td>";
$prezzo_2 = number_format($row[prezzo], 0, ',', '.');
$html .= "<td class='col_1'>";
$html .= "$prezzo_2";
$html .= "</td>";
$html .= "<td class='col_3'>";
$html .= "<a href='' class='a_mod_t' rel='$row[id]'><img src='icons/mod.png' border='0' title='modifica trattamento'/></a>";
$html .= "</td>";
$html .= "<td class='col_3'>";
$html .= "<a href='' class='a_mod_prezzo' rel='$row[id]'><img src='icons/euro.png' border='0' title='modifica prezzo'/></a>";
$html .= "</td>";
$html .= "<td class='col_3'>";
$html .= "<a href='' class='a_delete_t' rel='$row[id]'><img src='icons/delete.png' border='0' title='cancella trattamento'/></a>";
$html .= "</td>";
$html .= "<td class='col_4'>";
$html .= "<div class='chkbox_trattamenti'>";
$html .= "<form class='form_chkbox_trattamenti'>";
$html .= "<input type='checkbox' class='inpt_chkbox_trattamenti' value='1' rel='$row[id]'>";
$html .= "<input class='hidden_id_dottore_chk' type='hidden' value='<?php echo $id; ?>''>";
$html .= "<input class='hidden_id_paz_chk' type='hidden' value='<?php echo $paziente_id; ?>'>";
$html .= "<input class='hidden_id_chk' type='hidden' value='<?php echo $trattamento_id; ?>'>";
$html .= "</form>";
$html .= "<div class='chkmk_img_trattamenti'>";
$html .= "<img src='icons/checkmark.png' border='0' title='trattamento chiuso' />";
$html .= "</div>";
$html .= "</div>";
$html .= "</td>";
您需要获得current
div的对象。
创建一个名为successMsg
的变量,并将当前对象赋值给它,如下所示
var successMsg = $(this);
并在成功方法中调用它,像这样$(successMsg).parent().parent().find('.chkmk_img_trattamenti').show();
这是更新后的JS
$(document).ready(function() {
$("#div_trattamenti").on('change', '.inpt_chkbox_trattamenti', function(e) {
e.preventDefault();
if ($(this).is(":checked")) {
var id = $(this).attr('rel');
var value = $(this).val();
alert(value); // prezzo
}
if ($(this).is(":not(:checked)")) {
var id = $(this).attr('rel');
var value = 0;
alert(value); // prezzo
}
var successMsg = $(this);
data_chk = 'id=' + id + '&value=' + value;
$.ajax({
type: "POST",
data: data_chk,
cache: false,
url: "php/aggiorna_chiuso.php",
success: function(html) {
var obj = $.parseJSON(html);
$.each(obj, function(index, element) {
var value2 = element.chiuso; // value chiuso
}); // fine .each
if (value == 1) {
$(successMsg).parent().parent().find('.chkmk_img_trattamenti').show();
//$('.chkmk_img_trattamenti', this).show();
}
if (value == 0) {
//$('.chkmk_img_trattamenti', this).hide();
$(successMsg).parent().parent().find('.chkmk_img_trattamenti').hide();
}
}, // fine success
error: function(xhr, status, error) {
console.log(status);
console.log(error);
console.dir(xhr);
} // fine error
}); // fine ajax
});
});
$html = '';
$html .= "<table class='class_table'>";
while( $row = mysql_fetch_array($query_1)){
$html .= "<tr class='row'>";
$html .= "<td class='col_1'>";
$html .= "$row[prestazione]";
$html .= "</td>";
$html .= "<td class='col_2'>";
$html .= "$row[denti]";
$html .= "</td>";
$prezzo_2 = number_format($row[prezzo], 0, ',', '.');
$html .= "<td class='col_1'>";
$html .= "$prezzo_2";
$html .= "</td>";
$html .= "<td class='col_3'>";
$html .= "<a href='' class='a_mod_t' rel='$row[id]'><img src='icons/mod.png' border='0' title='modifica trattamento'/></a>";
$html .= "</td>";
$html .= "<td class='col_3'>";
$html .= "<a href='' class='a_mod_prezzo' rel='$row[id]'><img src='icons/euro.png' border='0' title='modifica prezzo'/></a>";
$html .= "</td>";
$html .= "<td class='col_3'>";
$html .= "<a href='' class='a_delete_t' rel='$row[id]'><img src='icons/delete.png' border='0' title='cancella trattamento'/></a>";
$html .= "</td>";
$html .= "<td class='col_4'>";
$html .= "<div class='chkbox_trattamenti'>";
$html .= "<form class='form_chkbox_trattamenti'>";
$html .= "<input type='checkbox' class='inpt_chkbox_trattamenti' value='1' rel='$row[id]'>";
$html .= "<input class='hidden_id_dottore_chk' type='hidden' value='<?php echo $id; ?>''>";
$html .= "<input class='hidden_id_paz_chk' type='hidden' value='<?php echo $paziente_id; ?>'>";
$html .= "<input class='hidden_id_chk' type='hidden' value='<?php echo $trattamento_id; ?>'>";
$html .= "</form>";
$html .= "<div class='chkmk_img_trattamenti'>";
$html .= "<img src='icons/checkmark.png' border='0' title='trattamento chiuso' />";
$html .= "</div>";
$html .= "</div>";
$html .= "</td>";
隐藏/显示元素:
$("#div_trattamenti").on('change', '.inpt_chkbox_trattamenti', function (e) {
.
.
.
$("#" + this.id).find(" .chkmk_img_trattamenti").show();
.
.
.
});