1

这是我遇到每个人的问题的代码。自从我不懂 js 以来,这就是我已经工作了 8 个小时的工作。我从 w3school.com 网站获得了大部分内容,并且一直在尝试将所有内容拼凑在一起。呃,请帮忙

<script type="text/javascript">
var stringToMatch = 'christopher',  
input = document.getElementById('text'),  
div = document.getElementById('divColor1');  

 function toggle (switchElement){  
 if (this.value == stringToMatch){  
    div.style.display = 'block';  
}  
else {  
    div.style.display = 'none';  
}  
};?  
 </script>


 <!--This is the start of the code which i want to appear--> 
 <h3>Coupon Redeem</h3> 
 <form action="test" method="post"> 
Please type in the Coupon Code you have recieved: 
 <form id='coupon' onChange="javascript: ShowMenu(document.getElementById('text').value,'divColor1');">  
<input id="text" type="text"> 
<input type="button" id="text" value="Redeem" onClick="toggle(this)" /> 
</form>
 <!--This is the start of the hidden field that i want to appear when the coupon code is entered     correctly--> 
<div id="divColor1" style="display:none;"> 
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> 
<input type="hidden" name="cmd" value="_s-xclick"> 
<input type="hidden" name="hosted_button_id" value="asjsanxci"> 

  <input type="hidden" name="on0" value="Tokens">Tokens 
    <select name="os0"> 
  <o ption value="5 tokens">5 tokens$5.00 USD</option> 
 <option value="10 tokens">10 tokens$10.00 USD</option> 
 <option value="15 tokens">15 tokens$13.00 USD</option> 
 <option value="20 tokens">20 tokens$18.00 USD</option> 
 <option value="25 tokens">25 tokens$23.00 USD</option> 
 <option value="30 tokens">30 tokens$27.00 USD</option> 
 <option value="35 tokens">35 tokens$33.00 USD</option> 
 <option value="40 tokens">40 tokens$38.00 USD</option> 
  <option value="50 tokens">50 tokens$45.00 USD</option> 
 </select>  
&nbsp;&nbsp;&nbsp;&nbsp; 
<input type="hidden" name="on1" value="Username Verification">Username Verification 
 <input type="text" name="os1" maxlength="200"> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<input type="hidden" name="currency_code" value="USD"> 
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"> 
</form> 
</div> 

<br> 
</form> 
4

2 回答 2

1

修复代码

displayed:none => display:none

var input = document.getElementById('text'); 
// your html don't have id='text' 

<input type="text"> => <input id='text' type="text">

你必须解决两个问题

有用!

祝你好运。

于 2012-07-30T12:28:28.670 回答
1

您的代码有语法错误

<div id="divColor1" style="displayed:none"

将其更改为

<div id="divColor1" style="display:none;">

还有一些我发现的其他错误

<form id='coupon' onChange="javascript: ShowMenu(document.getElementById('text').var,'divColor1',);">

您的 ShowMenu 缺少一个变量,以及另一个语法错误document.getElementById('text').var

>

<form id='coupon' 
    onChange="javascript: ShowMenu(document.getElementById('text').value,'divColor1');">

更新 我对您的代码进行了一些更改。你可以在这里看到工作代码

更新代码

<script>
var stringToMatch = 'christopher';  
function toggle (){  
 var input = document.getElementById('text').value; 

 if (input == stringToMatch){ 
    document.getElementById('divColor1').style.display = 'block';  
}  
else {  
   document.getElementById('divColor1').style.display = 'none';  
}  
};​
</script>

 <!--This is the start of the code which i want to appear--> 
 <h3>Coupon Redeem</h3> 
 <form action="test" method="post"> 
Please type in the Coupon Code you have recieved: 
 <form id='coupon' onChange="javascript: ShowMenu(document.getElementById('text').value,'divColor1');">  
<input id="text" type="text"> 
<input type="button" id="text" value="Redeem" onClick="toggle()" /> 
</form>
 <!--This is the start of the hidden field that i want to appear when the coupon code is entered     correctly--> 
<div id="divColor1" style="display:none;"> 
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> 
<input type="hidden" name="cmd" value="_s-xclick"> 
<input type="hidden" name="hosted_button_id" value="asjsanxci"> 

  <input type="hidden" name="on0" value="Tokens">Tokens 
    <select name="os0"> 
  <o ption value="5 tokens">5 tokens$5.00 USD</option> 
 <option value="10 tokens">10 tokens$10.00 USD</option> 
 <option value="15 tokens">15 tokens$13.00 USD</option> 
 <option value="20 tokens">20 tokens$18.00 USD</option> 
 <option value="25 tokens">25 tokens$23.00 USD</option> 
 <option value="30 tokens">30 tokens$27.00 USD</option> 
 <option value="35 tokens">35 tokens$33.00 USD</option> 
 <option value="40 tokens">40 tokens$38.00 USD</option> 
  <option value="50 tokens">50 tokens$45.00 USD</option> 
 </select>  
&nbsp;&nbsp;&nbsp;&nbsp; 
<input type="hidden" name="on1" value="Username Verification">Username Verification 
 <input type="text" name="os1" maxlength="200"> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<input type="hidden" name="currency_code" value="USD"> 
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"> 
</form> 
</div> 

<br> 
</form> ​
于 2012-07-30T12:30:03.200 回答