我已經爲我的頁面編寫了一個基本的文本編輯器。任何具有.edit類的標籤都可以通過登錄用戶進行點擊,並且除了解除「點擊」之外,還會出現一個表單,其中textarea字段中的文本將被編輯。提交後,函數執行一個帖子並通過ColdFusion組件更新數據庫,並重新加載頁面以反映所做的更改。很好的工作(謝謝不少於Stackoverflow社區和Ray Camden)。如何在腳本中停止多個帖子?
我創建了一個'取消'功能,清除表單字段(因爲它是可重用的),並重新綁定'點擊'。麻煩的是,我在螢火蟲中看到,如果我點擊一些文本,取消,點擊其他文本,然後點擊提交我的代碼執行兩個職位 - 一個用於取消點擊,一個用於實際。
我需要做些什麼來殺死第一次中止的努力?
下面的代碼:
$(document).ready(function() {
$('#cancelEdit').click(rebinder);
$('.edit').click(texteditor);
function texteditor(){
var theName = $(this).attr('name');
var textToEdit = $(this).html();
var theParentID = $(this).closest('div').attr('id');
var theTag = this.tagName.toLowerCase();
var theID = theName.split("-")[1];
var gateway = ("<cfoutput>#URL.page#</cfoutput>");
var fieldDims = [$(this).css('width'), $(this).css('height')];
$('#theText').attr('value', textToEdit);
$('#theText').css('height', fieldDims[1]);
$('#theText').css('width', fieldDims[0]);
$('.edit').unbind('click');
$('#texteditform').submit(function(e){
//stop the form submission
e.preventDefault()
var newText = $('#theText').val();
//CFC
$.post("cfc/engine.cfc?method=updateText&returnformat=json",
{text:newText, field:theID, gateway_id:gateway},
function(res) {
//Handle the result
if(res.trim() == "true") {
$('#theText').attr('value', '');
location.reload();
//$('.edit').bind('click');
} else {
alert("Didn't Work");
}
});
});
};
function rebinder(){
alert('rebinder');
$('.edit').click(texteditor);
$('#theText').attr('value', '');
$('#theText').css('height', '');
$('#theText').css('width', '');
};
// End of document.ready function //
});
我只是看着控制檯,不僅後發生兩次,但它從第二次嘗試都崗位的努力,不幸的是更新與這兩個元素職位文本.edit類。這是很糟糕......
所以,基本上,如果我採取#texteditform。提交併使其成爲一個獨立的功能,並將texteditor的值傳遞給它,這將解決它? – Ofeargall 2010-07-14 17:54:14
非常多 - 我要做的是在點擊它之後,我會在texteditor()函數中爲該單擊的元素添加一個類(讓我們說'點擊')。再加上我將綁定在$(document).ready()函數中的提交,並使它與類'點擊'的元素,並獲取信息(你需要'你從元素名稱獲得'ID'參數)從它作爲:$('。clicked')。attr('name');. – Jan 2010-07-14 18:00:51