首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript -在未选中Radio时更改元素内容

Javascript -在未选中Radio时更改元素内容
EN

Stack Overflow用户
提问于 2019-04-08 21:18:53
回答 3查看 103关注 0票数 1

当未检查无线电输入时,如何更改内容?当内容发生变化时,是否有一种方式来激活过渡?

代码语言:javascript
复制
function radioCheck(target) {
	radioElem = document.getElementById(target);
}

function statusChange(target) {
	label = document.getElementById(target);
	if (radioElem.checked) {
		label.innerHTML = "done";
	}
	else {
		label.innerHTML = "copy";
	}
}
代码语言:javascript
复制
<form>
    <input type="radio" name="checkfield" id="radio-1"  onchange="radioCheck('radio-1');statusChange('label-1');"/><label for="radio-1" id="label-1">copy</label>
     <input type="radio" name="checkfield" id="radio-2"  onchange="radioCheck('radio-2');statusChange('label-2')"/><label for="radio-2" id="label-2">copy</label>
     <input type="radio" name="checkfield" id="radio-3"  onclick="radioCheck('radio-3');statusChange('label-3')"/><label for="radio-3" id="label-3">copy</label>
</form>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-04-08 22:37:40

解释1

  • radCheck()传递事件对象
    • rads是一个实时的HTMLCollection
      • event.currentTarget引用<form>
      • .elements是所有窗体控件的集合。
      • .checkfield将集合缩小到所有名为checkfield的窗体控件。

  • for...of循环遍历rads - r是当前无线电。
代码语言:javascript
复制
- Ternary:  _**if**_ _current radio is_ _**`.checked`**_:  status = "Done" _**else**__: status = "Copy"_

.textContent设置为状态值(即“复制”或“已完成”)

这是为每一对收音机和标签做的。

解释2

  • document.forms[0] -第一个(并且只在本例中) <form>
  • .onchange -将<form>注册到更改事件
  • radCheck -调用事件处理程序 /*参见解释1 */ const radCheck = event => { const = event.currentTarget.elements.checkfield;for ( rads) { let radCheck= r.checked?“已完成”:“复制”;r.nextElementSibling.textContent =状态;} /*参见说明2 */ document.forms.onchange = radCheck;
票数 2
EN

Stack Overflow用户

发布于 2019-04-08 21:21:47

是像这样吗?

代码语言:javascript
复制
function radioCheck(target) {
	radioElem = document.getElementById(target);
}

const labels = document.querySelectorAll("label"); // added

function statusChange(target) {
	label = document.getElementById(target);
        labels.forEach(function(p){ // added
          p.innerHTML = "copy"; // added
        }); // added
	if (radioElem.checked) {
		label.innerHTML = "done";
	}
	else {
		label.innerHTML = "copy";
	}
}
代码语言:javascript
复制
<form>
    <input type="radio" name="checkfield" id="radio-1"  onchange="radioCheck('radio-1');statusChange('label-1');"/><label for="radio-1" id="label-1">copy</label>
     <input type="radio" name="checkfield" id="radio-2"  onchange="radioCheck('radio-2');statusChange('label-2')"/><label for="radio-2" id="label-2">copy</label>
     <input type="radio" name="checkfield" id="radio-3"  onclick="radioCheck('radio-3');statusChange('label-3')"/><label for="radio-3" id="label-3">copy</label>
</form>

票数 2
EN

Stack Overflow用户

发布于 2019-04-08 21:51:32

这一策略与@dgknca's answer中的策略非常相似,但从最初的问题中得到了一些概括和澄清。

代码语言:javascript
复制
// Add the event listener to each input
document.querySelectorAll('input').forEach(input => {
  input.addEventListener('change', onRadioChange);
})

function onRadioChange(e) {
  // Set each label to the inital value
  document.querySelectorAll('input').forEach(input => {
    if (input !== e.target) input.labels[0].innerHTML = 'copy'
  })
  
  // Set the selected input's label to the checked value
  e.target.labels[0].innerHTML = 'done'
}
代码语言:javascript
复制
<form>
  <input id="input-1" type="radio" name="checkfield" />
  <label for="input-1">copy</label>
  
  <input id="input-2" type="radio" name="checkfield" />
  <label for="input-2">copy</label>

  <input id="input-3" type="radio" name="checkfield" />
  <label for="input-3">copy</label>
</form>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55581897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档