使用最近的div上的trigger()强制点击 [英] Force click with trigger() on closest div

查看:428
本文介绍了使用最近的div上的trigger()强制点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有以下代码:

$('.myInput').click(function() {
  $('.addon').trigger('click');
});

$('.addon').click(function() {
  console.log("Clicked");
});

.wrapper {
  display: flex;
  flex-direction: column;
}
.inputWithAddon {
  display: flex;
  margin-bottom: 10px;
}
input {
  height: 20px;
}
.addon {
  width: 26px;
  height: 26px;
  background-color: green;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="inputWithAddon">
    <input class="myInput">
    <div class="addon"></div>
  </div>
  <div class="inputWithAddon">
    <input class="myInput">
    <div class="addon"></div>
  </div>
  <div class="inputWithAddon">
    <input class="myInput">
    <div class="addon"></div>
  </div>
</div>

当我点击输入时,在我的绿色div上强制点击 trigger()。这应该在当前点击的输入的div上(在右边)。目前,它调用所有绿色div的点击(看控制台)。我想只为这样做的点击输入的div。我尝试了 closest()

I would like to force a click with trigger() on my green div, when I clicked in the input. This should be on the div from the current clicked input (on the right side). At the moment, it calls the click for all the green divs (look at console). I would like just to do this for the div of the clicked input. I tried closest():

$(this).closest('.addon').trigger('click');

无效。

任何想法?

感谢。

推荐答案

尝试使用sibling属性of Jquery

Try using the sibling property of Jquery

$(this).siblings('.addon').trigger('click');

这篇关于使用最近的div上的trigger()强制点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆