如何在jQuery中执行click()

How to execute click() in jQuery

本文关键字:click 执行 jQuery      更新时间:2024-06-02

这段代码基本上采用"textmoney"来计算一个人一年的收入。要执行此功能,必须按enter键。我还有一个名为"moneydiv"的div,它将执行相同的函数,但如果我在同一个脚本中同时具有这两个函数,则两者都不起作用。我该如何编写代码让用户能够同时执行这两个功能?如何使用div对click()进行编码?

Jquery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>
    $(document).ready(function() {
    var $demo = $('#demo');
    var $textMoney = $('#textmoney');
    var $moneydiv = $('#moneydiv');
    .keydown(function(e) {
         if (e.keyCode === 13) {
            var money = $(this).val();
            if (isNaN(money) || money === '') {
                $demo.text('You aint enter no $$$$$$');
            } 
            else {
                var dailyE = $(this).val() / 365;
                $demo.text('$' + dailyE + ' per day');
                 }
            } 
           else if ($(this).val() === '') {
            $demo.text('');
            }
           }).mouseover(function() {
           $(this).css('border', '1px solid black');
           }).mouseout(function() {
           $(this).css('border', '1px solid grey');
        });
  });
    </script>

如果您想使用点击功能

$("#id").click(function () {
 // your code here
})

我建议访问Jquery API了解更多信息。

希望得到帮助。

我想当你点击"moneydiv"时,你想触发"textmoney"的press事件,你可以这样做。

  1. 注册"textmoney"新闻发布会
$('#textmoney').keydown(function(e) {
  if (e.keyCode === 13) {
    var money = $(this).val();
    if (isNaN(money) || money === '') {
      $demo.text('You aint enter no $$$$$$');
    } else {
      var dailyE = $(this).val() / 365;
      $demo.text('$' + dailyE + ' per day');
    }
  } else if ($(this).val() === '') {
    $demo.text('');
  }
}).mouseover(function() {
  $(this).css('border', '1px solid black');
}).mouseout(function() {
  $(this).css('border', '1px solid grey');
});
  1. 然后在单击moneydiv时触发按键事件
var keyPressEvent = $.Event('keypress');
keyPressEvent.keyCode = 13;
$('#moneydiv').trigger(keyPressEvent);

然而,另一个简单的实现是,您可以将逻辑提取到函数中,并将函数绑定到单击和按键事件。

希望能对你有所帮助!

如果moneydiv和textmoney需要执行相同的函数。那么我建议你这样做。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
    var $demo = $('#demo');
    var $textMoney = $('#textmoney');
    var $moneydiv = $('#moneydiv');
    function todo(){
        var money = $textMoney.val();
            if (isNaN(money) || money === '') {
                $demo.text('You aint enter no $$$$$$');
            } else {
                var dailyE = $textMoney.val() / 365;
                $demo.text('$' + dailyE + ' per day');
            }
    }
    // on enter key
    $textMoney.keydown(function(e) {
        if (e.which === 13) {
            todo();
        } else if ($(this).val() === '') {
            $demo.text('');
        }
    }).mouseover(function() {
        $(this).css('border', '1px solid black');
    }).mouseout(function() {
        $(this).css('border', '1px solid grey');
    });
    // on click 
    $moneydiv.click(function(){    
        todo();
    });
});
</script>

希望能有所帮助。