我是法國人,所以藉口一些翻譯錯誤......
我有一個代碼,使用jQuery和Ajax調用,我不明白以下問題。
我有一張表,第一行。 在該行的末尾,有兩個選項:有效和刪除。 當你點擊有效的圖標時,出現一個加載圖標,出現一個新行,附加到表中,加載圖標消失,並顯示刪除圖標。
當您點擊刪除圖標時,它會刪除該行。
所以我的問題......
在開始的時候,我實現了一個「的onclick =有效的()」和「的onclick =」上的圖標刪除()」直接。 但現在,我覺得更乾淨使用:
$('#valid'+i).click(function(){
valid(i);
});
$('#delete'+i).click(function(){
delete(i);
});
,問題出現:當你點擊,例如在刪除圖標線3條,這是誰出現在4號線裝載...
有代碼
PHP/HTML
<?php
include_once 'functions_sandbox.php';
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.0.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
//Fonction qui charge les datepicker et les différentes options sur les champs
function reload_jquery(){
$('input[type=text][id*=tags]').each(function() {
i = $(this).attr('num');
$(this).css('border','thin blue solid');
$('#valid'+i).click(function(){
valid_temps(i);
});
$('#delete'+i).click(function(){
delete_temps(i);
});
});
}
function valid_temps(i){
$('#loading'+i).show();
$('#valid'+i).hide();
var variables = 'mode=saisie_temps&j='+$('#j').val();
$.ajax(
{
// Define AJAX properties.
type: "POST",
url: "/dgi/ajax_sandbox.php",
data : variables,
dataType: "html",
timeout: 6000,
// Define the succss method.
success: function(data)
{
$('#data_retour').append(data);
$('#loading'+i).hide();
$('#delete'+i).show();
$('#j').val(parseInt($('#j').val())+1);
reload_jquery();
}
}
);
}
function delete_temps(i){
$('#loading'+i).show();
$('#delete'+i).hide();
$("#confirm").dialog({
resizable: false,
height:160,
width: 350,
modal: true,
buttons: {
"Oui": function() {
supp_temps(i);
$(this).dialog("close");
},
"Non": function() {
$('#loading'+i).hide();
$('#delete'+i).show();
$(this).dialog("close");
}
}
});
}
function supp_temps(i){
$('#ligne'+i).hide('slow');
}
$(function(){
reload_jquery();
});
</script>
<table id="data_retour" class="module">
<?php echo ligne_saisie_sandbox(0); ?>
</table>
<div style="display:none" id="confirm" title="Confirmation">Voulez-vous vraiment supprimer ce temps ?</div>
<label>Indice J : </label>
<input type="text" value="1" id="j" name="j" />
</body>
</html>
在 'functions_sandbox.php'
function ligne_saisie_sandbox($j){
$retour = '
<tr id="ligne'.$j.'">
<td class="cellule _light">
<label>Attraction'.$j.' : </label>
<input type="text" id="tags'.$j.'" num="'.$j.'" />
<img src="/dgi/img/b_save.png" alt="Valid" title="Valider votre temps" title="Valider le temps" id="valid'.$j.'" style="margin-top:5px;float:right">
<img src="/dgi/img/b_empty.png" alt="Supprimer" title="Supprimer votre temps" title="Supprimer le temps" id="delete'.$j.'" style="margin-top:5px;float:right;display:none">
<img src="/dgi/img/ajax_clock_small.gif" alt="Load" id="loading'.$j.'" style="margin-top:5px;float:right;display:none;" />
</td>
</tr>';
return $retour;
}
和Ajax的在 「ajax_sandbox.php」
include_once 'functions_sandbox.php';
switch ($_POST['mode'])
{
case "saisie_temps":
$j = $_POST['j'];
$retour = ligne_saisie_sandbox($j);
echo $retour;
break;
}
小圖像的 「ligne_saisie_sandbox」 功能來說明問題
完美!我將「var」添加到我的變量中,我使用了HTML5屬性,並且它工作正常。 – 2013-03-08 10:21:56