简体   繁体   English

触发模糊事件而不是单击

[英]Blur event is triggered instead of click

When a user starts typing into an input field, the web app I'm working on generates drop down with auto-fill options, if a user clicks on one of the options, it populates the corresponding input fields with auto-fill text.当用户开始在输入字段中输入内容时,我正在开发的 Web 应用程序会生成带有自动填充选项的下拉菜单,如果用户单击其中一个选项,它会使用自动填充文本填充相应的输入字段。

The web app contains many invoice lines, each having their own hidden auto-fill dropdown that is hidden until auto-fill options are available. Web 应用程序包含许多发票行,每行都有自己隐藏的自动填写下拉菜单,在自动填写选项可用之前一直隐藏。

If a user clicks on an autofill option, I would like to update the order with this auto-fill text.如果用户单击自动填充选项,我想使用此自动填充文本更新订单。 If the user does not click on an autofill option and goes on to the next invoice line, I would still like to update the orderArray with the plain text the user entered.如果用户没有点击自动填充选项并继续下一个发票行,我仍然想用用户输入的纯文本更新orderArray

To accomplish this, I tried using a blur event, however this blur event triggers regardless of whether or not a dropdown option was clicked, and I need to to be triggered if and only if a dropdown option from its corresponding line was not clicked .为此,我尝试使用blur事件,但是无论是否单击了下拉选项,都会触发此 blur 事件,并且当且仅当未单击相应行中的下拉选项时才需要触发该事件。

I understand why the blur event is always getting triggered, however I am having an extremely difficult time overcoming this problem, by separating the two events and updating the order correctly.我理解为什么模糊事件总是被触发,但是我很难通过分离两个事件并正确更新顺序来克服这个问题。

I appreciate any suggestions.我很感激任何建议。

$(".dropdown").on(click, ".item-option", function(){                
    //set item object with dropdown option text
    ...
    ...
    ...
    
    updateOrder(true, item);
    $('.dropdown').hide();

});
//if customer enters custom information without clicking on item option
$('.id-input, .name-input, .price-input, .qty-input').blur(function(){

       updateOrder(false, {});
});
        
        
function updateOrder(clicked, item){
   if(clicked==true){
       //update order with the item information from the dropdown the user clicked
    }else{
      //update order by collecting all plaintext user entered into input fields
    }
}

Okay Have a look at these changes that I've made in your JS: I've Observed That:好的,看看我在您的 JS 中所做的这些更改: 我观察到:
click event triggers after the blur .在 blur 之后触发 click 事件
Instead of click use mousedown it will work.而不是单击使用mousedown它将起作用。

Here Are The changes I've made in your JS:以下是我在您的 JS 中所做的更改:

$(".dropdown").on("mousedown", ".item-option", function(e){                
  e.stopPropagation();
//set item object with dropdown option text
...
...
...

updateOrder(true, item);
$('.dropdown').hide();
return false;

});
//if customer enters custom information without clicking on item option
$('.id-input, .name-input, .price-input, .qty-input').blur(function(){

   updateOrder(false, {});
});


function updateOrder(clicked, item){
  if(clicked==true){
   //update order with the item information from the dropdown the user clicked
  }else{
  //update order by collecting all plaintext user entered into input fields
}
}

Hope It'll Help.. Cheers :)..希望它会有所帮助..干杯:)..

Try below code.试试下面的代码。

$(".dropdown").on(click, ".item-option", function(event){                
   //set item object with dropdown option text
   ...
   ...
   ...
   updateOrder(true, item);
   $('.dropdown').hide();
   event.stopPropagation();
});

//if customer enters custom information without clicking on item option
$('.id-input, .name-input, .price-input, .qty-input').blur(function(){
   updateOrder(false, {});
});


function updateOrder(clicked, item){
  if(clicked==true){
     //update order with the item information from the dropdown the user clicked
  } else{
    //update order by collecting all plaintext user entered into input fields
  }
}

stopPropagation is required only in click event because, once dropdown option clicked, blur also triggered, which we need to stop. stopPropagation 仅在单击事件中是必需的,因为一旦单击下拉选项,模糊也会触发,我们需要停止。 Blur does not trigger click, so stopPropagation not needed there.模糊不会触发点击,因此那里不需要 stopPropagation。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM