2011-01-14 121 views
0

那麼,基本上我有以下代碼(我使用jQuery)。我使用下面的代碼通過ajax post動態更新字段。所有工作正常,但當我使用代碼動態生成內容與多個輸入字段時,該代碼不起作用。我使用「活」,但似乎這不起作用。任何解決方案提前致謝。Ajax編輯框問題

我的HTML代碼:

<span class="text_wrapper"> 
<span class="imageInf"><a href="tag/1235">1235</a></span> 
<a href="javascript:;" class="edit_link" title="Add/Edit"> 
<img src="i/edit.png" class="middle"> 
</a> 
</span> 
<span class="edit" style="display:none"><input type="text" class="editbox" name="mediatags" value="1235"></span> 

我的JavaScript代碼:

//ajax edit box 
$(document).ready(function() 
{ 
//Edit link action 
$('.edit_link').live("click", function() 
{ 
$('.text_wrapper').hide(); 
var data=$('.text_wrapper').html(); 
$('.edit').show(); 
$('.editbox').html(data); 
$('.editbox').focus(); 
}); 
//Mouseup textarea false 
$(".editbox").mouseup(function() 
{ 
return false 
}); 
//Textarea content editing 
$(".editbox").live("change", function() 
{ 
$('.edit').hide(); 
var boxval = $(".editbox").val(); 
var dataString = 'data='+ boxval; 
$.ajax({ 
type: "POST", 
url: "editPicName.php", 
data: dataString, 
cache: false, 
success: function(html) 
{ 
$('.text_wrapper').html(boxval); 
$('.text_wrapper').show(); 
} 
}); 
}); 
//Textarea without editing. 
$(document).mouseup(function() 
{ 
$('.edit').hide(); 
$('.text_wrapper').show(); 
}); 
}); 

回答

1

如果你想在你的POST請求傳遞超過1個參數,您需要在 「數據」 包括選項你的$ .ajax函數。

所以說你要傳遞2個輸入字段「數據1」和「數據2」

的值您$就調用看起來像這樣

 

$.ajax({ 
    type: "POST", 
    url: "editPicName.php", 
    data: "data1=value1&data2=value2", 
    cache: false, 
    success: function(html) 
    { 
    $('.text_wrapper').html(boxval); 
    $('.text_wrapper').show(); 
    } 
}); 

 

這裏我假設「值1」和「value2」是這些輸入字段的當前值。

您也可以將$ .ajax調用的數據選項發送爲JSON。我建議你這樣做(它看起來更乾淨)

 

$.ajax({ 
    type: "POST", 
    url: "editPicName.php", 
    data: ({data1 : "value1", data2 : "value2"}), 
    cache: false, 
    success: function(html) 
    { 
    $('.text_wrapper').html(boxval); 
    $('.text_wrapper').show(); 
    } 
});