[英]changing span value when checkbox is checked and unchecked
I want to have a span value change when a checkbox is checked, then changed back to the original value when unchecked.我想在选中复选框时更改跨度值,然后在未选中时更改回原始值。
<span id="shipping">Standard</span>
<input class="form-check-input" type="checkbox" name="ship" value="Expedited" id="exp1">
The original value is 'Standard'.原始值为“标准”。 I want to change this to 'Expedited' when checked or 'Standard' if unchecked.
我想在选中时将其更改为“加急”或未选中时将其更改为“标准”。 How do I go about doing this?
我该怎么做?
Try like this.像这样尝试。 add a eventListener to that checkbox and check the status and change its value.
向该复选框添加一个 eventListener 并检查状态并更改其值。
document.getElementById('exp1').addEventListener('click', function(){ if(this.checked){ this.value = 'Expedited';; document.getElementById('name').innerHTML = 'Expedited'; } else{ this.value = 'Standard'; document.getElementById('name').innerHTML = 'Standard'; } });
<input class="form-check-input" type="checkbox" name="ship" value="Standard" id="exp1"><span id="name">Standard<span>
You can create two text child span
with with Standard
& Expedited
and a class hiddden
.您可以使用
Standard
& Expedited
和一个 class hiddden
创建两个文本子span
。 Then on change of the checkbox get all the child span text and add or remove the hidden
class using toggle
然后更改复选框获取所有子跨度文本并使用
toggle
添加或删除hidden
类
document.getElementById('exp1').addEventListener('change', function(e) { changeDisplay() }) function changeDisplay() { document.querySelectorAll('.spanTxt').forEach(function(elem) { elem.classList.toggle('hidden') }) }
.hidden { display: none; }
<span> <span class="spanTxt">Standard</span> <span class="spanTxt hidden">Expedited</span> </span> <input class="form-check-input" type="checkbox" name="ship" value="Expedited" id="exp1">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.