首页 > 解决方案 > innerHTML 不改变 div 输出

问题描述

我正在尝试根据用户单击的单选按钮显示文本区域。当“竞赛”功能被注释掉时,时事通讯部分工作正常。但是当包含竞赛功能时,时事通讯不起作用。我试过调试,但似乎找不到问题。我尝试过使用不同的形式,但这并没有改变问题。

我的 HTML:

function newsletter() {
	var response = "";
	if(document.getElementById("yes").checked) {

		response += "<p><b>Enter Your Address:</b></p>";
		response += '<input type="text" id="address"><br>';

		var output = document.getElementById("isChecked");
		output.innerHTML = response;
 }
} 

function contest() {
	var resp = "";
	if(document.getElementById("answer").value == "Y") {
		resp += "<p><b>Enter your credit card information to verify age ($0.00 charge)</b></p>";
		resp += "<br>";
		resp += '<input type="text" id="first4" size="4" maxlength="4">';
		resp += "-";
		resp += '<input type="text" id="second4" size="4" maxlength="4">';
		resp += "-";
		resp += '<input type="text" id="third4" size="4" maxlength="4">';		
		resp += "-";
		resp += '<input type="text" id="fourth4" size="4" maxlength="4"';

		var out = document.getElementById("contestOutput");
		out.innerHTML = resp;
 }
}
body {background-color: pink; }
		<center>
			<h1>Magnificant Music!</h1>
		</center>
		<p>Welcome Blue Note Records visitors! On this site, you can enter your information to recieve a card sent every month informing you about the lastest releases on your favorite record label, and a chance to enter a contest that could win you a brand new instrument of your choice!</p>
		<form action="" method="post">
			<fieldset>
				<p><b>Personal Information</b></p>
				<label>First Name:</label>
				<input type="text" id="firstName"><br>
				<label>Last Name:</label>
				<input type="text" id="lastName"><br>
				<label>Middle Initial</label>
				<input type="text" id="middleInit"><br>
			</fieldset>
			<br>
			<fieldset>
				<p><b>Do you want to recieve a newsletter?</b></p>
				<input type="radio" name="news" id="yes">Yes<br>
				<input type="radio" name="news" id="no">No<br>
				<button type="button" onclick="newsletter();">Submit</button>
				<div id="isChecked"></div>
			</fieldset>
			<br>
			<fieldset>
				<p><b>Would you like to enter the contest for a brand new instrument of your choice (Y / N)? (18 yrs old minimum)</b></p>
				<input type="text" size="1" id="answer"><br>
				<button type="button" onclick="contest();">Submit</button>
				<div id="contestOutput"></div>
			</fieldset>
		</form>

标签: javascripthtml

解决方案


@jcubic 提供的答案是错误。关闭标签修复了遇到的错误。


推荐阅读