jquery 切换按钮值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5701948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery toggle button value
提问by Rajeev
In the below code when the play button is clicked Its value should be changed to pause and when pause is clicked the other function should be called .How to do this using jquery toggle
在下面的代码中,当点击播放按钮时,它的值应该更改为暂停,当点击暂停时,应该调用另一个函数。如何使用 jquery 切换来做到这一点
<input type="button" onclick ="play" value="play"/>
<script>
function play()
{
play_int();
// Button value to be changed to pause
}
And when pause play_pause();
回答by Town
Give your button an ID:
给你的按钮一个 ID:
<input type="button" id="play" value="play"/>
Then you can do something like this:
然后你可以做这样的事情:
$('#play').click(function() {
if ($(this).val() == "play") {
$(this).val("pause");
play_int();
}
else {
$(this).val("play");
play_pause();
}
});
Or a slightly tidier version like this:
或者像这样稍微整洁的版本:
$(function(){
$('#play').click(function() {
// if the play button value is 'play', call the play function
// otherwise call the pause function
$(this).val() == "play" ? play_int() : play_pause();
});
});
function play_int() {
$('#play').val("pause");
// do play
}
function play_pause() {
$('#play').val("play");
// do pause
}
回答by diEcho
回答by Pierre de LESPINAY
I would say
我会说
statuses = ['Play', 'Pause'];
$('#btn_play').val(
$('#btn_play').val() == statuses[0]
? statuses[1] : statuses[0]
);
回答by Muhamad Bhaa Asfour
$('#play_button_id').toggle(play,play_pause);
this will trigger the play() function when you click the button for the first time
这将在您第一次单击按钮时触发 play() 函数
and the play_pause() when you click the button for the second time
以及第二次单击按钮时的 play_pause()
回答by Starx
A simplest idea can be this
一个最简单的想法可以是这个
function play(action) {
{
if(action=='play') {
play_int();
$("#btn_play").val('pause');
}
else {
pause_int();
$("#btn_play").val('play');
}
}
$("#btn_play").click(function() {
val = $(this).val();
play(val);
}