选择输入标签复选框时显示文本 [英] Display text when input tag checkbox is selected
问题描述
我尝试在用户选择输入
标签时显示文本,
I am trying to display a text when the user selects the input
tag,
例如,具有以下代码:
<tr>
<td class="align">
Yes <br/>
<input type="checkbox" />
</td>
</tr>
用户单击复选框后,将显示以下文本:
Once the user clicks on the check box, the following text will appear:
如果您选择了,
"If you have selected yes,....."
我不确定如果JavaScript是执行此操作的最佳路径。
And I am not sure if JavaScript would be the best path to do this.
它会是这样的:
But, If it is, would it be something like:
<script>
$(function(){
if(input was checked)//not sure what would go here, new to Javascript
display results//not sure either, new to Javascript
});
</script>
推荐答案
您将要连接 onchange 监听器复选框并让它调用你的函数。
You will want to hook up an onchange listener to the checkbox and have that call your function.
以下是一些可以帮助您开始使用的代码片段。
Here is some code snippet that will get you started.
var resultEle = document.getElementById("result");
function showAnswer(ele) {
if(ele.checked) {
result.innerHTML = "You checked Yes!";
}
else {
result.innerHTML = "";
}
}
<table>
<tr>
<td class="align">
Yes<br />
<input type="checkbox" onchange="showAnswer(this)" />
</td>
<td>
<span id="result"></span>
</td>
</tr>
</table>
像你使用JQuery,我的答案是纯Javascript。我强烈建议你在使用JQuery之前学习一些vanilla javascript,这样你就可以完全理解幕后发生了什么。 MDN有一个伟大的初学者指南! https://developer.mozilla.org/en-US/docs/Web / JavaScript / Guide
Looks like you are using JQuery, my answer is plain Javascript. I would strongly suggest you learn some vanilla javascript before using JQuery so that you can fully understand what is happening behind the scenes. MDN has a great starters guide! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
这篇关于选择输入标签复选框时显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!