编辑*****
嗨,我的代码第一次正常工作,但第二次,我的对象函数返回undefined。
PS:我编辑了这篇文章,为了更好地理解我的长篇剧本。 PS2:提前谢谢你:)
_MANAGE = new Object()
//----------------------------- BG ------------------------------//
var saveBg = {
name : "bg",
url : "AN URL",
data : {
ID :function($this){ return $this.parents("tr").data("id") },
name :function($this){ return $this.parents("tr").find(".name").val() }
}
}
//----------------------------------------------------------------------//
//------------------------- SAVE FUNCTION ------------------------------//
var saveButton = [saveBg];
$(".buttonSave").on("click", function(){
console.log(saveBg);
var buttonName = $(this).data("name");
_MANAGE.saveButton(saveButton, buttonName, $(this));
})
//----------------------------------------------------------------------//
//------------------------- SAVE BUTTON ------------------------------//
_MANAGE.saveButton = function(saveButton, buttonName, $this_button){
for(var i = 0; i < saveButton.length; i++){
if(buttonName == saveButton[i]["name"]){
for(var param in saveButton[i]["data"]){
if(typeof(saveButton[i]["data"][param]) == "function"){
saveButton[i]["data"][param] = saveButton[i]["data"][param]($this_button);
} else {
saveButton[i]["data"][param] = $(saveButton[i]["data"][param]).val();
}
}
if(!saveButton[i]["success"]){
saveButton[i]["success"] = function() {
$this_button.children()
.hide()
.replaceWith("<span style='font-size:0.7em;''>Saved</span>").fadeIn("slow");
setInterval(function(){
$this_button.children().hide().replaceWith('<span class="icon-floppy"> </span>').fadeIn("slow");
}, 1500);
}
}
_MANAGE.ajaxButton(saveButton[i]);
}
}
}
//----------------------------------------------------------------------//
//------------------------- ASYNC FUNCTION ------------------------------//
_MANAGE.ajaxButton = function(ajaxButton) {
var stringData = "";
if(ajaxButton.data){
var loop = 0;
for(var param in ajaxButton["data"]){
if(loop == 0){
stringData += param + "=" + ajaxButton["data"][param];
} else {
stringData += "&" + param + "=" + ajaxButton["data"][param];
}
loop++;
}
}
if(ajaxButton.url && ajaxButton.success && stringData != "") {
$.ajax({
url : ajaxButton.url,
data : stringData,
dataType : "text",
success : ajaxButton.success
});
}
}
<!--THE TABLE -->
<tr data-id="4">
<td><b>4</b></td>
<td><input class="name" type="text" value="A VALUE"></td>
<td>
<!-- THE BUTTON -->
<div class="buttonSave" data-name="bg">
<span class="icon-floppy"></span>
</div>
</td>
</tr>