简体   繁体   English

如何在单击按钮时更改onclick函数值

[英]How to change the onclick function value upon clicking a button

I've tried looking for answer for this and can't seem find one specific to this. 我已经尝试寻找这个问题的答案,似乎找不到一个特定的。

My site has a button, which looks like this: 我的网站有一个按钮,如下所示:

<input class="btn btn-success" onclick="w.start()" type="button" value="Start" id='start' />

And on click I want it to change to "Stop" and I want the onclick function to change to onclick="w.stop()" instead of w.start() and I want the class to change to from btn-success to btn-danger (bootstrap) 点击后我希望它改为“停止”,我希望onclick函数更改为onclick="w.stop()"而不是w.start() ,我希望类更改为从btn-successbtn-danger (bootstrap)

I can get the name to change fine using this jQuery: 可以使用这个jQuery来改变名称:

$(function () {
    $('#start').click(function () {
        $(this).val() == "Start" ? play_int() : play_pause();
    });
});

function play_int() {
    $('#start').val("Stop");
    // do start
}

function play_pause() {
    $('#start').val("Start");
    // do stop
}

But how do I get the onclick function and the class to change? 但是如何让onclick函数和类更改?

Especially the onclick function :) 特别是onclick功能:)

You got a lot of good advice about how to handle the issue in different ways -- but nothing answering your specific question about how to change the onclick . 关于如何以不同方式处理问题,您有很多好的建议 - 但没有回答您关于如何更改onclick具体问题。 This is a useful ability in many contexts -- so even if you take somebody else's counsel about the general approach, I wanted to answer your specific question. 在许多情况下,这是一种有用的能力 - 因此,即使你采取其他人关于一般方法的建议,我也想回答你的具体问题。

Here's code that I've tested and found to work: 这是我测试过并发现有效的代码:

<script>
function play_int() {
    alert("play_int was executed");
    $("#start").val("Stop");
    $("#start").attr("onclick", "play_pause()");
    // do start
}

function play_pause() {
    alert("play_pause was executed");
    $("#start").val("Start");
    $("#start").attr("onclick", "play_int()");
    // do stop
}
</script>

<input class="btn btn-success" onclick="play_int();" type="button" value="Start" id='start' />

I think it would be easiest to just start with two buttons, one called start and one called stop, and have stop initially hidden. 我认为最简单的方法是从两个按钮开始,一个叫做start,一个叫stop,最初是隐藏的。

In your start function add 在你的开始功能添加

$('#start').hide();
$('#stop').show();

Then your old button, with its name and function disappear and your new button with the proper name and proper function appears. 然后,您的旧按钮及其名称和功能将消失,并显示具有正确名称和正确功能的新按钮。

Instead of changing the function it is calling, use an "if" statement. 不要改变它调用的函数,而是使用“if”语句。 Not as familiar with jquery as I am raw, js, but I think it would be something like this. 不像jquery那样熟悉jsery,js,但我认为它会是这样的。

if($('#start').val = "start"){
   function play_int() {
        $('#start').val("Stop");
        // do start
   }
}
if($('#start').val = "stop"){
   function play_pause() {
        $('#start').val("Start");
        // do stop
   }
}
$(function(){$('#start').click(function() {
   if($(this).val() == "Start"){
       $(this).val("Stop");
       play_int();
   } else {
       $(this).val("Start");
       play_pause();
   }
});

Just use one event handler. 只需使用一个事件处理程序 I would use jQuery to detect which class is active on your button. 我会使用jQuery来检测按钮上哪个类是活动的。 Then you can perform the appropriate actions. 然后,您可以执行适当的操作。 You don't need to set onclick in your element if you use this. 如果使用此元素,则无需在元素中设置onclick。

$(function () {
    $('#start').on('click', function() {
        if($(this).is('.btn-success')){
            $(this).removeClass('.btn-success').addClass('.btn-danger').val('Stop');
            //Do Start
        }else{
            $(this).removeClass('.btn-danger').addClass('.btn-success').val('Start');
            //Do Stop
        }
    });
});

Why not call a toggle function each time your button is clicked and let it handle the conditional. 为什么不在每次单击按钮时调用切换功能并让它处理条件。

function toggleChange(event){
  if($('#start').val()=="Stop";
     $('#start').val("Start");
     $('#start').addClass('newClass')
     $('#start').removeClass('oldClass')
   }
   else{
      $('#start').val("Stop");
      $('#start').addClass('newClass')
      $('#start').removeClass('oldClass')
   }
}
<input class="btn btn-success" onclick="w.start()" type="button" value="Start" id='btn' />

 $(function () {
  $('#btn').click(function () {
    var opn=$(this).val();
    switch(opn){
      case 'start':
      $(this).val("stop").removeClass('.btn-success').addClass('.btn-danger').click(w.stop); 
      break;

      case 'stop':
      $(this).val("start").removeClass('.btn-danger').addClass('.btn-success').click(w.start); 
      break;

  }
});
});

This is a no jquery solution 这是一个没有jquery的解决方案

HTML HTML

<input class="btn-success" onclick="callme(this);" type="button" value="Start"/>

JAVASCRIPT JAVASCRIPT

var callme = (function() {
    var start = true;
    return function(me) {
        if(start) {
            me.value = "Stop";
            me.className = "btn-danger";
            w.start();
        } else {
            me.value = "Start";
            me.className = "btn-success";;
            w.stop();
        }
        start = !start;
    };
}());

Here is the fiddle http://jsfiddle.net/HhuNU/ 这是小提琴http://jsfiddle.net/HhuNU/

I took a look at your responses and found a solution that fits best for me: 我看了一下你的回答,找到了一个最适合我的解决方案:

$(function(){$('#start').click(function() {
    $(this).val() == "Start" ? play_int() : play_pause();                           
    });
});

function play_int() {
    $('#start').val("Stop");
    $('#start').addClass('btn-danger');
    $('#start').removeClass('btn-success');
    w.start();
}

function play_pause() {
    $('#start').val("Start");
    $('#start').addClass('btn-success');
    $('#start').removeClass('btn-danger');
    w.stop();
}

The w.stop() and w.start() are functions of another bit of js that starts the timer (this is for a timer app) :) w.stop()和w.start()是启动计时器的另一个js的函数(这是一个计时器应用程序):)

Feel free to give me feedback on my solution. 随意给我反馈我的解决方案。

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

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