私は次のhtmlを持っています:
<select name="user_level" id="user_level" class="input" onChange="javascript:choosePackage(this.value);" >
<option value="1" >Single</option>
<option value="3" >Multi</option>
<option value="7" >Super</option>
</select>
<div id="showPackageSelect" ></div>
そしてこれは私のjsです:
<script type="text/javascript">
function choosePackage(packageselected){
if(packageselected=='3') { $("#showPackageSelect").replaceWith("<div id='normal'>User Package</div><select name='user_package' id='user_package' class='input'><?php $rPackages = mysql_query('select * from packages WHERE id>=2 AND id <=5 ORDER BY id DESC');while ($rp = mysql_fetch_array($rPackages)) { ?><option value='<?php echo $rp['id'];?>'><?php echo $rp['package'];?></option><?php } ?></select>");
} else if(packageselected=='1') { $("#showPackageSelect").replaceWith("<div id='normal'>User Package</div><select name='user_package' id='user_package' class='input'><option value='1'>Super User</option></select>");
} else if(packageselected=='6') { $("#showPackageSelect").replaceWith("<div id='normal'>User Package</div><select name='user_package' id='user_package' class='input'><option value='6'>Single</option></select>");
} else { alert('this should never happen!'); }
}
</script>
if elseなどを使用していくつかの方法を試しました。面白いことに、値をアラートすると正常に機能し、テストすると1回実行されますが、その後は単一に戻りません。
よろしくお願いしますジョニー
アップデート:
<script type="text/javascript">
function choosePackage(packageselected){
if(packageselected=='3')
$("#showPackageSelect").replaceWith("<div id='showPackageSelect' ><div id='normal'>User Package</div><select name='user_package' id='user_package' class='input'><?php $rPackages = mysql_query('select * from packages WHERE id>=2 AND id <=5 ORDER BY id DESC');while ($rp = mysql_fetch_array($rPackages)) { ?><option value='<?php echo $rp['id'];?>'><?php echo $rp['package'];?></option><?php } ?></select></div>");
else if(packageselected=='1')
$("#showPackageSelect").replaceWith("<div id='showPackageSelect' ><div id='normal'>User Package</div><select name='user_package' id='user_package' class='input'><option value='1'>Super User</option></select></div>");
else if(packageselected=='7')
$("#showPackageSelect").replaceWith("<div id='showPackageSelect' ><div id='normal'>User Package</div><select name='user_package' id='user_package' class='input'><option value='6'>Single</option></select></div>");
}
</script>