Javascript显示窗体的隐藏变量

Javascript Show hide variable for form

本文关键字:隐藏 变量 窗体 显示 Javascript      更新时间:2024-04-22

这是我遇到的每个人都有问题的代码。这是我已经工作了8个小时,因为我不知道js。我大部分都是从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> 

您的代码出现语法错误

<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> ​

修复代码

displayed:none => display:none
var input = document.getElementById('text'); 
// your html don't have id='text' 
<input type="text"> => <input id='text' type="text">

你必须解决两个问题,然后

它有效!

祝你好运。