php如何给select下拉框赋值
a.php页面顶部有部分html代码,有个表单,表单有select、input、button,
表单提交给自己:form action="" method="post"
现在有个问题,每次页面提交后,select的值都回到默认值,想保持所选择的值不变。设计的代码如下:
button 的onclick事件使用js代码
<script> <br />function check() {<br /> var t = document.getElementById("bbsty"); //bbsty是select的id<br /> var seleValue=t.options[t.selectedIndex].value; //获取select的值 <br /> document.form1.submit(); <br /> for(i=0;i<t.length;i++){ //给select赋值 <br /> if(t.options[i].value== selvalue){ <br /> t.options[i].selected=true; <br /> }<br /> }<br />}<br /></script>
我在页面提交前取select值了,提交后再给select赋回去,但是没能成功,于是我想到使用隐藏域做中间变量。
<script><br /> var sele = document.getElementById("bbsty"); <br /> var seleValue=sele.options[sele.selectedIndex].value;//获取select的值<br /> document.getElementById("sel").value=seleValue; //将本页面的bbsty(select)值赋值给上面的隐藏域<br /></script>
function check() {
document.form1.submit();
var selvalue = document.getElementById("sel");
var t = document.getElementById("bbsty");
for(i=0;i
t.options[i].selected=true;
}
}
}
但为什么还是不能成功啊!请高人给看看,我的思路对不对,代码哪块有问题,该咋实现这个功能啊?
------解决方案--------------------
思路问题,
<br />document.form1.submit(); <br />
这东西执行之后,不在于你用hidden的input或者是用js变量保存的值,后边的东西都是白瞎的,因为你页面已经跳转了,等于你重新加载了当前的页面。
所以,如果你想让
<br />document.form1.submit(); <br />
后边的代码执行,必须保证当前页面没有被重新加载,这才应该是你的思路,而不是通过那种方式保存值的问题。
提示:你可以改一下form的target,让form提交到一个宽高为0的iframe里,这样,你本身页面不会被重新加载。这样才能解决你的问题
其实这个问题还有很多其他办法解决,建议有时间去想一想。
------解决方案--------------------
方法很多,这是一种
<?php<br />$bbsty = '';<br />if($_POST) {<br /> $bbsty = $_POST['bbsty'];<br />}<br />?><br /><form method=post><br /><select name=bbsty><br /><option value='a'>a</option><br /><option value='b'>b</option><br /><option value='c'>c</option><br /><option value='d'>d</option><br /></sclect><br /><input type=submit value=ok><br /></form><br /><script><br />document.forms[0].bbsty.value = '<?php echo $bbsty; ?>';<br /></script><br />

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。