简体   繁体   English

第二次单击后触发按钮的功能

[英]Button's function triggered after second click

I have two buttons and one input. 我有两个按钮和一个输入。 The input takes the value of the clicked button. 输入采用单击按钮的值。 Both buttons have the same class, have one click (Vue) for add active class, and second click. 两个按钮具有相同的类别,单击一次(Vue)可添加活动的类别,然后单击第二次。 On click take the button's value and append it in the input. 单击时,获取按钮的值并将其附加在输入中。

The problem with which I am stuck is that the function for taking the value works after the second triggered click. 我遇到的问题是,在第二次触发点击后,用于获取值的函数才能正常工作。

I've tried with dblclick and to delete the click for active class - but nothing changed. 我已经尝试过dblclick并删除活动类的单击-但没有任何更改。 Can someone give a hand with this? 有人可以帮忙吗? Thank you. 谢谢。

Vue project (js function imported in methods) Vue项目(在方法中导入的js函数)

<div class="col-md-12 buttons-wrapper" id="direction">
  <button id="buy-button" value="1" name="Direction1" class="button btn buy" @click="selected = 1" :class="{active:selected == 1}" v-on:click.capture="buttonDirectionValue">Buy</button>
  <button id="sell-button" value="2" name="Direction2" class="button btn sell" @click="selected = 2" :class="{active:selected == 2}" v-on:click.capture="buttonDirectionValue">Sell</button>
  <input id="inputDirection" name="Direction" type="text" placeholder="" class="form-control input-md" style="display: block" readonly></input>
</div>
buttonDirectionValue() {
  event.preventDefault();
  $("#direction button").click(function() {
    $("#inputDirection").val($(this).val());
  });
buttonDirectionValue() {
    event.preventDefault();
    $("#direction button").trigger("click");
});
$("#direction button").click(function() {
    $("#inputDirection").val($(this).val());
});

Above should be your code. 上面应该是您的代码。 Correction is to place the .click outside of buttonDirectionValue() function. 纠正措施是将.click放置在buttonDirectionValue()函数之外。

If you place the .click inside of buttonDirectionValue() function, the .click will only be registered when the button is clicked for the first time and triggered on the second click. 如果将.click放置在buttonDirectionValue()函数的内部,则只有在第一次单击该按钮并在第二次单击时触发该.click时,才会注册.click。

While placing it outside will bind the .click on page load and trigger the click on buttonDirectionValue() function call. 将其放置在外部时,将绑定.click页面加载并触发clickDirectionValue()函数调用。

Hope this helps 希望这可以帮助

The problem obviously is that you bind the click event after the button click 问题显然是您在按钮单击后绑定了click事件。

you can bind the click event right after page loads like that: 您可以在页面加载后立即绑定click事件,如下所示:

    $(document).ready(function(){
        $("#direction button").click(function() {
            $("#inputDirection").val($(this).val());
        }
    });

//edit1 // EDIT1

you can also remove the v-on:click.capture="buttonDirectionValue" 您还可以删除v-on:click.capture =“ buttonDirectionValue”

and use this code to bind click events for both buttons 并使用此代码绑定两个按钮的点击事件

    $(document).ready(function(){
        $('button.buy, button.sell').click(function() {
            $("#inputDirection").val($(this).val());
        }
    });

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

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