我正在創建自己的作爲AJAX的成績冊。在它的底部有一個可編輯單元格的表格。用戶點擊單元格輸入等級,當他們點擊單元格時,等級通過AJAX發送到數據庫。到目前爲止,它工作的很好,除了我已經添加了用戶按Enter的功能並使用戶在其他地方單擊以關閉編輯表單時的行爲。jquery模糊運行兩次 - 爲什麼?
的問題是,當用戶點擊返回,而不是輸入,模糊部分運行兩次被警告彈出兩次證明。如果他們只是在某處點擊,那很好。 我對jQuery的.blur()的理解是,如果您在沒有回調或參數的情況下調用它,它就像一個doer一樣,並將它看作是所選元素失去焦點。
適用於IE8,Chrome和FF 4.0.1。在my test site
現場運行的版本是否有人可以解釋爲什麼它的運行兩次,當我嘗試當用戶點擊進入設置模糊?
更新:無法發佈HTML,因爲它實際上只是一個表,並且表標記集不在stackOverflow白名單中。 (我是新來的,所以也許有辦法做到這一點,但我不知道。)
而且,我解決了這個緊迫的問題,通過改變
if(event.keyCode=='13')
{
$('#gradeUpdate').blur();
}
到
if(event.keyCode=='13')
{
$("#gradeUpdate").parent().focus();
//$('#gradeUpdate').blur();
}
但我仍然想知道爲什麼原始線不會讓#gradeUpdate模糊,就像我認爲的那樣。
一切都發生在這個函數內:
function clickableCell(){
$("td.assignmentCell").click(function(){ //if a td with an assignment class is clicked,
if(clicked == 0)
{
clicked = 1;
currentValue = $(this).text();//get the current value of the entered grade
var passable = this;
alert("Test: passable is: "+$(passable).text());
//change content to be an editable input form element
$(this).html("<input name='gradeUpdate' id='gradeUpdate' size=3 value='"+currentValue+"' type='text' />");
//move the cursor to the new input and highlight the value for easy deletion
$('#gradeUpdate').focus().select();
//watch for keystrokes somewhere else and act appropriately
$(document).keyup(function(event){
//if they hit Enter, treat it like they clicked somewhere else
if(event.keyCode=='13')
{
$('#gradeUpdate').blur();
}
});
$('#gradeUpdate').blur(function(passable){
//reset the clicked counter
clicked = 0;
//check to see if the value is blank or hasn't changed
var inputValue = $('#gradeUpdate').val();
//////////////////////////////////////////////////////////////////////////////////////////
// Here we need to insert a REGEX check for the "exception" values created by the GDST
// and check for those values; anything else that's not a number will be disallowed
// and reset to "" so that it's caught in a later step. For now I'm just checking for digits
//////////////////////////////////////////////////////////////////////////////////////////
if(!inputValue.match(/^\d+$/))
{
alert ("we don't have a match!");
inputValue = "";
}
///////////////////////////////////////////////////////////////////////////////////////////
if(currentValue == inputValue || inputValue =="")//hasn't changed or is blank
{
//DON'T run AJAX call
alert("Not a good value, reverting to old value!");
//assign the original, unchanged value to the table
$('#gradeUpdate').parent().text(currentValue)
$("#gradeUpdate").remove();//close out the input block
//make it like they actually clicked on the element they did click on to lose focus
$(this).click();
}
else //it's valid, send the ajax
{
//send AJAX call here
//on success update the td
alert("We're all good--entering value!");
$("#gradeUpdate").parent().text(inputValue);
$("#gradeUpdate").remove();
}
});
}//close of if clicked ==0
});
}
和這裏的原始頁面的完整HTML;它實際上只是一個預先輸入的值用於測試的表格。我的下一步是使用從AJAX請求返回的XML即時創建表。
除非我誤解你的意思,它不是真正的重新綁定在每一次點擊,因爲點擊變種表現得像一個布爾值開關。如果用戶點擊,此代碼將按預期工作;問題出現時,他們擊中輸入,而不是點擊某處。 – jcanker 2011-05-14 18:28:56
@jcanker:每當你說'$(x).click(function(){/*...*/})',你都綁定一個新的點擊處理程序。對於'$(x).blur(函數)'也是類似的,看起來'blur'會被多次綁定,即使'click'沒有。 – 2011-05-14 18:49:15