我尝试根据同一页面上的表单选择创建段落的实时预览。我一直在使用这段代码。
<script type="text/javascript">
function yesnoCheck() {
if (document.getElementById('yesCheck').checked) {
document.getElementById('ifYes').style.display = 'block';
}
else document.getElementById('ifYes').style.display = 'none';
}
</script>
Yes <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="yesCheck"> No <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="noCheck"><br>
<div id="ifYes" style="display:none">
If yes, where: <input type='text' id='yes' name='yes'><br>
When? <input type='text' id='acc' name='acc'>
</div>
我想知道的结果是会有一个预先准备好的段落,它会出现在表格的底部,并会根据上述选项的选择进行更改。喜欢,如果是,是的&#39;如果选中,则会出现:
先生。 X将出现在&#39; London&#39;在&#39; 8 am&#39;
如果&#39;否&#39;如果选中,则会出现:
先生。 X将缺席
提前感谢您的帮助。
答案 0 :(得分:1)
尝试以下方法:
<html>
<head>
</head>
<body>
Yes <input type="radio" onclick="yesnoCheck();" name="yesno" id="yesCheck"> No <input type="radio" onclick="yesnoCheck();" name="yesno" id="noCheck"><br>
<div id="ifYes" style="display:none">
If yes, where: <input type='text' onchange="onLocationChange();" id='yes' name='yes'><br>
When? <input type='text' id='acc' onchange="onTimeChange();" name='acc'>
<div>
Mr. X will be present
<span id="location" style="display: none;"></span>
<span id="time" style="display: none;"></span>
</div>
</div>
<div id="ifNo" style="display:none">
Mr. X will be absent
</div>
<script>
function onLocationChange(){
var value = document.getElementById('yes').value;
if (value) {
document.getElementById('location').style.display = 'inline';
document.getElementById('location').innerHTML = ' in ' + value;
}
else {
document.getElementById('location').style.display = 'none';
}
}
function onTimeChange(){
var value = document.getElementById('acc').value;
if (value) {
document.getElementById('time').style.display = 'inline';
document.getElementById('time').innerHTML = ' at ' + value;
}
else {
document.getElementById('time').style.display = 'none';
}
}
function yesnoCheck() {
if (document.getElementById('yesCheck').checked) {
document.getElementById('ifYes').style.display = 'block';
document.getElementById('ifNo').style.display = 'none';
}
else {
document.getElementById('ifYes').style.display = 'none';
document.getElementById('ifNo').style.display = 'block';
}
}
</script>
</body>
</html>
答案 1 :(得分:0)
我希望这是你的要求
function yesnoCheck() {
if (document.getElementById('yesCheck').checked) {
document.getElementById('ifYes').style.display = 'block';
document.getElementById('targetDiv').innerHTML=""
}
else {
document.getElementById('targetDiv').innerHTML="Mr. X will be absent";
document.getElementById('ifYes').style.display = 'none';
document.getElementById('yes').value=""
document.getElementById('acc').value=""
}
}
function showResult(){
var yes=document.getElementById('yes').value
var acc=document.getElementById('acc').value
if(yes!="" && acc!="" ){
document.getElementById('targetDiv').innerHTML="Mr. X will be present in '"+yes+"' at '"+acc+"'"
}
else{
alert("Input are blank")
}
}
&#13;
<!DOCTYPE html>
<html>
<body>
Yes <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="yesCheck"> No <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="noCheck"><br>
<div id="ifYes" style="display:none">
If yes, where: <input type='text' id='yes' name='yes'><br>
When? <input type='text' id='acc' name='acc'>
<button type="button" onclick="showResult()">Show Result</button>
</div>
<div id="targetDiv"></div>
</body>
</html>
&#13;