简体   繁体   English

JQuery Post触发窗口上的点击事件

[英]JQuery Post Triggering Click Event on window

I'm doing a very simple Jquery powered event where the user clicks on two div elements, then an API call is made with the data from the selected items.我正在做一个非常简单的由 Jquery 驱动的事件,其中用户单击两个 div 元素,然后使用来自所选项目的数据进行 API 调用。 For some reason, The $.post function is triggering my $(".objects").click() call setting the window as this.出于某种原因, $.post 函数触发了我的 $(".objects").click() 调用,将窗口设置为这样。 I thought it was causing a double click at first (Because the click function depends on data from the element, which window does not have), until I did some debugging.起初我以为它会导致双击(因为单击功能取决于来自元素的数据,而哪个窗口没有),直到我进行了一些调试。 Here's my code :这是我的代码:

HTML : HTML :

<div class="option-selection" data-value="1">Option 1</div>
<div class="option-selection" data-value="2">Option 2</div>
<div class="option-selection" data-value="3">Option 3</div>
<div class="option-selection" data-value="4">Option 4</div>
... etc ...

Javascript : Javascript :

$(".option-selection").click(function(ev) {
    console.log("CLICK ACTIVATED:");
    console.log(this);
    ev.stopImmediatePropagation(); // I tried this to solve the double click. ev is undefined.
    ... rest of code ...
    select_item_2(this);
}
function select_item_2(element) {
    console.log("SELECTING ITEM 2");
    ... get data ...
    console.log("ATTEMPTING TO POST ...");
    $.post( ... );

}

Console Output :控制台输出:

CLICK ACTIVATED:
<div class=​"option-selection" data-value=​"5">​…​</div>​
SELECTING ITEM 1
CLICK ACTIVATED:
<div class=​"option-selection" data-value=​"21">​…​</div>​
SELECTING ITEM 2
ATTEMPTING TO POST ...
CLICK ACTIVATED:
Window {parent: Window, opener: null, top: Window, length: 1, frames: Window, …}
Uncaught TypeError: Cannot read property 'stopImmediatePropagation' of undefined
    at 1:133
    at i (jquery.min.js:2)
    at qt (jquery.min.js:2)
    at qt (jquery.min.js:2)
    at Object.<anonymous> (jquery.min.js:2)
    at Function.each (jquery.min.js:2)
    at qt (jquery.min.js:2)
    at qt (jquery.min.js:2)
    at qt (jquery.min.js:2)
    at qt (jquery.min.js:2)

Any ideas what's happening here and how to fix it ?任何想法这里发生了什么以及如何解决它? I know I can just check if the element is a dom object (I found a function for that), But this seems like it shouldn't be triggering like this in the first place.我知道我可以检查元素是否是一个 dom 对象(我找到了一个函数),但这似乎不应该首先像这样触发。

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

 var option_1; $(".option-selection").click(function(ev) { if(option_1) { // Check if first option selected if(option_1 == $(this)[0]) { // Check if first selected item is not the current item console.log("Option already selected."); } else { console.log("2 different options selected."); // Run your POST call here // Once your post call is done you can reset the option_1 value, so that users can select other option for next round if needed option_1 = null; console.log("Options reseted"); } } else { option_1 = $(this)[0]; console.log("New Option."); } });
 .option-selection { cursor: pointer; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="option-selection" data-value="1">Option 1</div> <div class="option-selection" data-value="2">Option 2</div> <div class="option-selection" data-value="3">Option 3</div> <div class="option-selection" data-value="4">Option 4</div>

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

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