1、从数据库取数据,进行不同的判断,传给js变量,进而加载页面时,执行不同的JS函数。

<!-- 
根据数据库存有的身份证信息是否为空,调用修改页面时初始不同的选项。(显示身份证 或者显示护照)
-->
<?php 
if($arrMember['idnumber']==''){
	$change ='1';
}else{
	$change ='2';
}

?>

<script type="text/javascript">
   
	    window.onload=function(){
	    	var change="<?php echo $change;?>";	    	
	    	
	    	if(change == '1'){		    		 
	    		change1();
	    	}
	    	if(change == '2'){	 	    		  		
	    		changeidnu();
		    }
	    
		}
	
</script>

<script>
function changeidnu(){	
  document.all.mxh2.style.visibility='visible';
  document.all.mxh1.style.visibility='hidden'; 

}
function change1(){	
  document.all.mxh2.style.visibility='hidden';
  document.all.mxh1.style.visibility='visible';
   
}
</script>


2、下边表单显示部分


<div class="control-group">
							     <label class="control-label">证件类型:    </label> 
							     <?php //echo $change;?>
							     <?php if($change=='1'){?>
									<label class="control-label" style="margin-left:-80px;"><input  name="zhengjian" value="1"  type=radio  onclick="changeidnu()" />身份证件</label>								
								
									<label class="control-label " style="margin-left:-30px;"><input  name="zhengjian" value="2"  type=radio  onclick="change1()" checked="checked"/>其他身份证件</label>
								 <?php }else{?>
								 
								 <label class="control-label" style="margin-left:-80px;"><input  name="zhengjian" value="1"  type=radio  onclick="changeidnu()" checked="checked"/>身份证件</label>							
								
									<label class="control-label " style="margin-left:-30px;"><input  name="zhengjian" value="2"  type=radio  onclick="change1()" />其他身份证件</label>
								 
								 <?php }?>
								 
								 
							</div>	
								
							<div class="control-group">
							     <p>
								 
								 
									<div id=mxh2 style="visibility: hidden;margin-left:85px;">身份证件:
										<div style="margin-left:75px;margin-top:-25px;"><input  type="text" name="idnu" value="<?=$arrMember['idnumber']?>" style="width:200px"/></div>							
									</div>
								  </p>
							</div>	
						
						     
		
							<div id=mxh1 style="visibility: hidden;margin-left:85px;margin-top:-40px;" >
									   
								 护照号码:	<div style="margin-left:75px;margin-top:-25px;"><input  type="text" name="passportnu" value="<?=$arrMember['passportnu']?>" style="width:200px"/></div><br><br>
								 出生日期: <div style="margin-left:75px;margin-top:-25px;"><input class="Wdate" onClick="WdatePicker()"   type="text" name="birthday"  style="width:200px" value="<?=$arrMember['birthday']?>" /></div><br><br>
								<div style="margin-top:-20px;">性       别:</div> 
								 
								  <?php if($arrMember['gender']=='1'){?>
								    <label class="radio" style="margin-top:-40px;margin-left:40px;">
								   		<input name="gender" type="radio" value="1" checked="checked" />男
								    </label>
           						    <label class="radio" style="margin-top:-40px;margin-left:-30px;">   
           						    	<input name="gender" type="radio" value="0"  />女
           						    </label>							
           						  <?php }?>		
           						  
           						   <?php if($arrMember['gender']=='0'){?>
								    <label class="radio" style="margin-top:-40px;margin-left:40px;">
								   		<input name="gender" type="radio" value="1"  />男
								    </label>
           						    <label class="radio" style="margin-top:-40px;margin-left:-30px;">   
           						    	<input name="gender" type="radio" value="0" checked="checked"/>女
           						    </label>							
           						  <?php }?>			
										
							 </div>





===========================================================================


另一种 可移值性强的代码。

<script language="JavaScript" type="text/JavaScript">
<!--
function showdiv(targetid,objN){
   
      var target=document.getElementById(targetid);
      var clicktext=document.getElementById(objN)

            if (target.style.display=="block"){
                target.style.display="none";
                clicktext.innerText="点击查看详细信息";
  

            } else {
                target.style.display="block";
                clicktext.innerText='关闭详细信息信息';
            }
   
}
-->
</script>


<!-- 
根据数据库存有的身份证信息是否为空,调用修改页面时初始不同的选项。(显示身份证 或者显示护照)
-->
<?php 
if($arrMember['idnumber']==''){
	$change ='1';
}else{
	$change ='2';
}

?>

<script type="text/javascript">
window.onload = showIDnum;

function showIDnum() {
  var form = document.forms.signupForm;
  var mye1 = null, mye2 = null;
  if (!form || !form.zhengjian) return;
  mye1 = document.getElementById("mxh1");
  mye2 = document.getElementById("mxh2");

  if (!mye1 || !mye2) return;
  if (form.zhengjian[0].checked == true) {
    mye1.style.display = "";
    mye2.style.display = "none";
  } else {
    mye1.style.display = "none";
    mye2.style.display = "";
  }

}
</script>


              <div class="control-group">
                    <label class="control-label">证件类型</label> 
                    <div class="controls">
                    	<label class="radio"><input name="zhengjian" value="1" type=radio onclick="showIDnum();" <?php if($change=='2') echo 'checked="checked"' ?> />身份证</label>								
                    	<label class="radio"><input name="zhengjian" value="2" type=radio onclick="showIDnum();" <?php if($change=='1') echo 'checked="checked"' ?>/>其他证件</label>
                    </div>
                  </div>
		
                  <div class="control-group" id="mxh1">
                    <label class="control-label">身份证号码</label> 
                    <div class="controls">
                    	<input class="input-xlarge" type="text" name="idnu" value="<?=$arrMember['idnumber']?>" />
                    </div>
                  </div>

     

                  <div id="mxh2">
                  <div class="control-group">
                  	<label class="control-label">护照号码</label> 
                    <div class="controls">
                    	<input class="input-xlarge" type="text" name="passportnu" value="<?=$arrMember['passportnu']?>" />
                    </div>
                  </div>

                  <div class="control-group">
                  	<label class="control-label">出生日期</label> 
                    <div class="controls">
                    	<input class="Wdate" onClick="WdatePicker()" type="text" name="birthday" value="<?=$arrMember['birthday']?>" />
                    </div>
                  </div>

                  <div class="control-group">
                  	<label class="control-label">性别</label> 
                    <div class="controls">
                    	<label class="radio"><input name="gender" type="radio" value="1" <?php if($arrMember['gender']=='1'){echo 'checked="checked"';} ?> />男</label>
                    	<label class="radio"><input name="gender" type="radio" value="0" <?php if($arrMember['gender']=='0'){echo 'checked="checked"';} ?> />女</label>
                    </div>
                  </div>
                  </div>



本文转载:CSDN博客