Jquery模糊运行了两次——为什么?
我正在努力创建自己的成绩册,它可以作为AJAX使用。其底部是一个带有可编辑单元格的表格。用户单击一个单元格以输入等级,当他们单击该单元格时,等级将通过AJAX发送到DB。到目前为止,它工作得很好,只是我添加了一个功能,用户可以点击Enter,让它像用户点击其他地方一样关闭编辑表单 问题在于,当用户点击return而不是enter时,模糊部分会运行两次,弹出两次警报就证明了这一点。如果他们只是点击某个地方,那就好了。 我对jQuery的.blur()的理解是,如果在没有回调或参数的情况下调用它,它将充当执行者,并将其视为所选元素失去焦点 发生在IE8、Chrome和FF 4.0.1上。实时版本运行于 有人能解释一下为什么当我试图设置模糊时,当用户点击回车时,它会运行两次吗 更新:无法发布HTML,因为它实际上只是一个表,表标记集不在stackOverflow白名单上。(我是新来的,也许有办法,但我不知道。) 此外,我还通过改变现状解决了眼前的问题Jquery模糊运行了两次——为什么?,jquery,loops,blur,Jquery,Loops,Blur,我正在努力创建自己的成绩册,它可以作为AJAX使用。其底部是一个带有可编辑单元格的表格。用户单击一个单元格以输入等级,当他们单击该单元格时,等级将通过AJAX发送到DB。到目前为止,它工作得很好,只是我添加了一个功能,用户可以点击Enter,让它像用户点击其他地方一样关闭编辑表单 问题在于,当用户点击return而不是enter时,模糊部分会运行两次,弹出两次警报就证明了这一点。如果他们只是点击某个地方,那就好了。 我对jQuery的.blur()的理解是,如果在没有回调或参数的情况下调用它,它
if(event.keyCode=='13')
{
$('#gradeUpdate').blur();
}
到
但我还是想知道为什么原始行不只是像我想的那样进行“等级更新”以模糊
此函数中发生的一切:
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
});
}
函数clickableCell(){
$(“td.assignmentCell”)。单击(function(){//如果单击了带有赋值类的td,
如果(单击==0)
{
单击=1;
currentValue=$(this).text();//获取输入成绩的当前值
var passable=此;
警报(“Test:passable是:”+$(passable.text());
//将内容更改为可编辑的输入表单元素
$(this.html(“”);
//将光标移动到新输入并突出显示该值,以便删除
$(“#等级更新”).focus().select();
//注意其他地方的击键,并采取适当的行动
$(文档).keyup(函数(事件){
//如果他们点击回车键,就像他们点击了其他地方一样
if(event.keyCode=='13')
{
$('#gradeUpdate').blur();
}
});
$('#gradeUpdate').blur(函数(可通过){
//重置单击的计数器
单击=0;
//检查该值是否为空或未更改
var inputValue=$('#gradeUpdate').val();
//////////////////////////////////////////////////////////////////////////////////////////
//在这里,我们需要插入一个正则表达式检查GDST创建的“异常”值
//并检查这些值;任何其他不是数字的都将被禁止
//并重置为“”,以便在稍后的步骤中捕获。现在我只是检查数字
//////////////////////////////////////////////////////////////////////////////////////////
如果(!inputValue.match(/^\d+$/)
{
警惕(“我们没有匹配!”);
inputValue=“”;
}
///////////////////////////////////////////////////////////////////////////////////////////
如果(currentValue==inputValue | | inputValue==“”)//未更改或为空
{
//不要运行AJAX调用
警报(“不是一个好值,正在恢复到旧值!”);
//将原始的、未更改的值指定给表
$('#gradeUpdate').parent().text(currentValue)
$(“#gradeUpdate”).remove();//关闭输入块
//让它看起来像他们实际上点击了他们点击过的元素而失去了焦点
$(此选项)。单击();
}
否则//如果有效,请发送ajax
{
//在这里发送AJAX呼叫
//运输署成功更新
警惕(“我们都很好——输入值!”);
$(“#成绩更新”).parent().text(inputValue);
$(“#等级更新”).remove();
}
});
}//如果单击,则关闭==0
});
}
这是原始页面的完整HTML;它实际上只是一个带有一些预先输入的值的表,用于测试。我的下一步是使用AJAX请求返回的XML动态构建表。每次单击
td.assignmentCell
时,您都要重新绑定blur
和keyup
事件。如果有意义,请仅尝试绑定一次,或者使用live
或delegate
进行#gradeUpdate
用你的HTML更新你的问题,我就能更好地了解你在做什么。我认为你需要将
$(“#gradeUpdate”).blur()
更改为$(“#gradeUpdate”)[0].blur()
。浏览器将采用不同于普通模糊的jQuery模糊。所以它会被触发两次。我也遇到了同样的事情,$('input')[0].blur()代码>行以某种方式修复了它,但我不明白它是如何工作的。我的代码是:
$('h1 input').live('blur', function(){
var text = $('h1 input').val();
if(text == ''){
alert('!');
$('h1').html('<p>' + originalProjectName + '</p><span></span>');
text = originalProjectName;
}
var id = document.location.hash.split('-')[1];
$('h1').html('<p>' + text + '</p><span></span>');
$.getJSON('json-project.php', { method: 'rename', id: id, name: text }, function(data) {
var type = document.location.hash.split('-')[0];
if(type == '#shoot'){
$('#scroller-shoots ul li[data-id=' + id + ']').html(text);
}
if(type =='#project'){
$('#scroller-projects ul li[data-id=' + id + ']').html(text);
}
});
});
$('h1 input').live('keydown', function(event) {
originalProjectName = $(this).val();
if ( event.which == 13 ) {
$(this)[0].blur();
}
});
$('h1 input').live('blur',function(){
var text=$('h1 input').val();
如果(文本=“”){
警惕(“!”);
$('h1').html(''+originalProjectName+'');
text=原始项目名称;
}
var id=document.location.hash.split('-')[1];
$('h1').html(''+text+'');
$.getJSON('json-project.php',{method:'rename',id:id,name:text},函数(数据){
var type=document.location.hash.split('-')[0];
如果(类型='#射击'){
$('#滚动条[data id='+id+']').html(文本);
}
如果(类型=='#项目'){
$('#scroller projects ul li[data id='+id+']').html(文本);
}
});
});
$('h1 input').live('keydown',函数(事件){
originalProjectName=$(this.val();
if(event.which==13){
$(此[0].blur();
}
});
它没有意义,因为$(this)
应该只引用一个选择器,而不是多个选择器
$('h1 input').live('blur', function(){
var text = $('h1 input').val();
if(text == ''){
alert('!');
$('h1').html('<p>' + originalProjectName + '</p><span></span>');
text = originalProjectName;
}
var id = document.location.hash.split('-')[1];
$('h1').html('<p>' + text + '</p><span></span>');
$.getJSON('json-project.php', { method: 'rename', id: id, name: text }, function(data) {
var type = document.location.hash.split('-')[0];
if(type == '#shoot'){
$('#scroller-shoots ul li[data-id=' + id + ']').html(text);
}
if(type =='#project'){
$('#scroller-projects ul li[data-id=' + id + ']').html(text);
}
});
});
$('h1 input').live('keydown', function(event) {
originalProjectName = $(this).val();
if ( event.which == 13 ) {
$(this)[0].blur();
}
});
var isTargetWindow = false;
function addEvent(obj, evt, fn) {
if (obj.addEventListener) {
obj.addEventListener(evt, fn, false);
}
else if (obj.attachEvent) {
obj.attachEvent("on" + evt, fn);
}
}
var isTargetWindow = false;
addEvent(window,"load",function(e) {
addEvent(window, "mouseout", function(e) {
e = e ? e : window.event;
var from = e.relatedTarget || e.toElement;
if (!from || from.nodeName == "HTML") {
// stop your drag event here
// for now we can just use an alert
isTargetWindow = true;
}
else {
isTargetWindow = false;
}
});
});
$(document).on( 'blur', 'input', function( e ) {
if( isTargetWindow ) {
return false;
}
// ... do something here
}