最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

HTML radio unchecked event - difference between onclick and onchange - Stack Overflow

programmeradmin2浏览0评论

I am trying to use html radio input with this code:

function change(e) {
  console.log(e.target.value, e.target.checked);
}
<label><input type="radio" name="test" onchange="change(event)" value="sel1"> radio 1</label>
<label><input type="radio" name="test" onchange="change(event)" value="sel2"> radio 2</label>

I am trying to use html radio input with this code:

function change(e) {
  console.log(e.target.value, e.target.checked);
}
<label><input type="radio" name="test" onchange="change(event)" value="sel1"> radio 1</label>
<label><input type="radio" name="test" onchange="change(event)" value="sel2"> radio 2</label>

what I expect is that on change selection the change function is called 2 times, 1 for unchecked and 1 for checked, instead is called only one time. Is it the correct behavior? how can I get the expected? In this way, what is the difference between on change and on click?

I tryied with edge/chrome

Share Improve this question edited Jan 29 at 19:15 Mister Jojo 22.6k6 gold badges25 silver badges44 bronze badges asked Jan 29 at 17:27 PerryPerry 1,2172 gold badges12 silver badges23 bronze badges 2
  • 2 I think you want the input event: developer.mozilla./en-US/docs/Web/API/Element/input_event. It fires on every change. – Poul Bak Commented Jan 29 at 19:19
  • uff... i see this is a duplicate question: stackoverflow/q/11173685/854279
发布评论

评论列表(0)

  1. 暂无评论