Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

javascript - 获取选中复选框的值?(Get the value of checked checkbox?)

So I've got code that looks like this:(所以我有代码看起来像这样:)

<input class="messageCheckbox" type="checkbox" value="3" name="mailId[]">
<input class="messageCheckbox" type="checkbox" value="1" name="mailId[]">

I just need Javascript to get the value of whatever checkbox is currently checked.(我只需要Javascript来获取当前检查的任何复选框的值。)

EDIT : To add, there will only be ONE checked box.(编辑 :要添加,只有一个复选框。)

  ask by Mandatory Programmer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

None of the above worked for me but simply use this:(以上都没有为我工作,只是使用这个:)

document.querySelector('.messageCheckbox').checked;

Happy coding.(快乐的编码。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...