如果选中,则更改复选框旁边的文本。HTML + JS 或 jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18422861/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:35:54  来源:igfitidea点击:

Change text next to checkbox IF checked. HTML + JS or jQuery

javascriptjqueryhtmlcheckbox

提问by 50dollanote

I just want to use a simple checkbox, for example, a checkbox with 'Please check this' next to it, then when it's clicked the text changes to 'Checked'

我只想使用一个简单的复选框,例如,旁边带有“请检查这个”的复选框,然后当它被点击时,文本会变成“已检查”

I've been looking all over for a simple solution, but struggling and can't figure it out - despite the simplicity.

我一直在寻找一个简单的解决方案,但一直在努力并且无法弄清楚 - 尽管很简单。

Can somebody help me?

有人可以帮助我吗?

回答by Webinan

Using jQuery:

使用jQuery:

$('#boxid').click(function() {
  if ($(this).is(':checked')) {
    $(this).siblings('label').html('checked');
  } else {
    $(this).siblings('label').html(' not checked');
  }
});

and in your HTML:

并在您的 HTML 中:

<input id="boxid" type="checkbox"><label for="boxid">not checked</label>

EDIT:

编辑:

Working fiddle: http://jsfiddle.net/zpzxM/

工作小提琴:http: //jsfiddle.net/zpzxM/

回答by Sanjeev Chauhan

JavaScript:

JavaScript:

function changeText(id){

  if ($('#'+id).is(':checked')) {
    $('#'+id +"+span").html('Checked');
  }else{
    $('#'+id+ "+span").html('Please check this.');
  }
}

CSS:

CSS:

<input type="checkbox" id="123" onclick="changeText(this.id);"><span>Please check this.</span>

"live Example"

“活生生的例子”