innerHTML不更改div输出

时间:2018-10-30 13:08:33

标签: javascript html

我正在尝试显示文本区域,具体取决于用户单击的单选按钮。当注释掉“竞赛”功能时,新闻稿部分可以正常工作。但是,当包含竞赛功能时,新闻通讯将不起作用。我尝试调试,但是似乎找不到问题。我尝试过使用其他形式,但这并不能解决问题。

我的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>

1 个答案:

答案 0 :(得分:2)

@jcubic提供的答案是错误。关闭标签可以解决遇到的错误。