简体   繁体   English

使用CSS选中/取消选中复选框时切换文本和文本框

[英]Toggle Text and Textbox when Checkbox is checked/unchecked using CSS

In my HTML program, I am trying to implement a feature where there is a text question, "Did you work out today?" 在我的HTML程序中,我正在尝试实现一个文本问题“您今天锻炼了吗?”的功能。 followed by a checkbox. 然后是一个复选框。 If the checkbox is checked yes, I want a new line to appear that asks, "How long did you work out?" 如果选中该复选框,则我希望出现一个新行,询问:“您锻炼了多长时间?” followed by a textbox. 然后是文本框。 I currently have the CSS that sort of accomplishes my goal, but it only toggles the textbox, and How long did you work out constantly shows. 我目前拥有可以实现我的目标的CSS,但是它只能切换文本框,并且可以显示出您持续工作了多长时间。 How can I hide the text along with the textbox? 如何隐藏文本和文本框? Here is my CSS code: 这是我的CSS代码:

function $_(IDS) { return document.getElementById(IDS); }
function toggle(Info) {
var CState = $_(Info);
if (CState.style.display != "none") { CState.style.display = "none"; }
                               else { CState.style.display = "inline"; }
}
function HideElem(IDS1, IDS2) {
  toggle(IDS1);
  toggle(IDS2);
}

function inputFocus(i){
    if(i.value==i.defaultValue){ i.value=""; i.style.color="#000"; }
}
function inputBlur(i){
    if(i.value==""){ i.value=i.defaultValue; i.style.color="#888"; }
}

And here is that implemented in HTML: 这是用HTML实现的:

Did you work out today? <div class="checkboxFour">
  <input type="checkbox" value="1" onchange = "HideElem('share','meal')" id="checkboxInput" name="" />
  <label for="checkboxInput"></label>
  </div>
  <span id= "share">
    </span> <br><br>How long did you work out? <input type="text" id="meal" name="meal" size="40" style="display:none;" 
    value="Example: 8.5 Hours" onfocus="inputFocus(this)" onblur="inputBlur(this)">

I am also using a stylized checkbox, if that changes things. 我还使用了一个风格化的复选框,如果这会改变情况。 Thanks for any help. 谢谢你的帮助。

You cannot add a css style to a text node(the caption) so wrap the caption and the input element in another element and apply the visibility to that wrapper element 您不能将css样式添加到文本节点(标题),因此将标题和输入元素包装在另一个元素中并将可见性应用于该包装元素

<input type="checkbox" value="1" onchange="HideElem('share','mean-ct')" id="checkboxInput" name="" />

then 然后

<span id="mean-ct" style="display:none;">
    How long did you work out?
<input type="text" id="meal" name="meal" size="40" value="Example: 8.5 Hours" onfocus="inputFocus(this)" onblur="inputBlur(this)" />
</span>

Demo: Fiddle 演示: 小提琴

Wrap your question with span tag and specify id for it.Try this: 使用span标记包裹您的问题并为其指定id 。尝试以下操作:

Html: HTML:

<span id="how" style="display:none;">How long did you work out? </span>

javascript: javascript:

if (CState.style.display != "none") { 
  CState.style.display = "none"; 
  document.getElementById("how").style.display = "none"; //hide the question
}
else { 
    CState.style.display = "inline"; 
    document.getElementById("how").style.display = "inline"; //display the question
}

Demo 演示版

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选中/取消选中复选框时切换文本 - Toggle text when checkbox is checked/unchecked 使用javascript选中和取消选中动态创建的复选框时如何切换动态创建的文本框 - How to toggle a dynamically created textbox when dynamically created checkbox is checked and unchecked using javascript Cakephp:选中/取消选中复选框时启用/禁用文本框 - Cakephp: Enabling/Disabling textbox when checkbox being checked/unchecked 选中/未选中值的复选框切换开关 - Checkbox Toggle Switch with Checked/Unchecked values 选中复选框时,使用innerHTML添加的文本未选中时不会删除 - Text added with innerHTML when checkbox is checked not removing when unchecked 基于复选框选中/取消选中的文本框值 - textbox values based on checkbox checked/unchecked 取消选中/选中复选框时,javascript在div中添加/删除文本 - javascript add/remove text in div when checkbox is unchecked/checked 选中/取消选中复选框时更改相应标签的文本 - change text of corresponding label when checkbox is checked/unchecked 如何使用ajax或jquery选中和取消选中时设置复选框的值 - How to set value of checkbox when checked and unchecked using ajax or jquery 当复选框被选中或复选框未选中删除值时,如何在另一个文本框中获得复选框值和文本框值的总和 - how can i get sum of checkbox value and textbox value in another textbox when checkbox is checked or if checkbox unchecked remove the value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM