想了想,这个和js其实不怎么相关,还是php问题。
问题背景:该页面左边是一些记录的列表,右边是很多空框,供填入一些信息,之后保存。然后点击左边记录的其中一项,相关信息就能在右边出现。上述功能我都已经实现了。
问题:右边很多空框中有一项是select下拉列表进行选择哪辆车。我的做法是从数据库中读取carNum(integer类型),以此作为select中option的value,而显示的值是数据库中对应该车辆的licenseNum(车牌号),上述用php循环的方法查询并且实现的。但我用js指定该id的select的值时,该select一点反应没有。我把这个select换成确定的select(即去除php循环,而是在htm中直接设定value和选项名称),则js可以指定该select的值,并显示出该值。
但我希望能循环设置该select,而且js能指定该值(显然,这样的话就可以动态显示了。)
该如何解决呢? 撒分,不够的话回帖或者私信给我,可另补。
直接上代码:
<!---下面是htm直接设定html的select的代码----><select id="showOrder_21" name="truck" class="dropdown" style="margin-left:0px !important; border:1px solid #BABABA !important;"> <option value ="1">QAZ123</option> <option value ="2">WSX234</option> <option value ="3">EDC345</option> <option value ="4">TGB456</option></select></td>
下面是采用循环的方式实现的select:
<select id="showOrder_21" name="truck" class="dropdown" style="margin-left:0px !important; border:1px solid #BABABA !important;"><?php $query = "select * from transport.truck order by carNum";$data = getAll($query);foreach($data as $each){?><option value="<?php echo $each['carNum']; ?> "><?php echo $each['licNum'];?></option><?php } ?></select></td>
下面是我用的js 部分:
<script type="text/javascript"> $(document).ready(function(){ $(".LeftTask").click(function(){ $(this).css("background","#A8A8A8"); $(this).siblings().css("background","white"); var orderID = $(this).find('input').val(); $.ajax({ type:"get", url:"main/ajax.php", data:{ orderID:orderID }, success:function(msg){ msg = eval("("+msg+")"); <!---当中删除大量不相关的显示---> $("#showOrder_21").val(msg.truck); } }); });}); </script>
大牛们,求指导!!!
怎么解决这个问题呢?求关键代码片段或网上案例,万分感谢!!!
回复讨论(解决方案)
$(".LeftTask").click(function(){
改成
$(".LeftTask").live("click",function(){ .......
后者的优势是可以"即使某dom节点是页面载入完成之后生成的,也能绑定事件".
参阅jquery手册.
$(".LeftTask").click(function(){
改成
$(".LeftTask").live("click",function(){ .......
后者的优势是可以"即使某dom节点是页面载入完成之后生成的,也能绑定事件".
参阅jquery手册.
哇,你来了啊,欢迎欢迎。立马试。
谢谢啊。
另外更好的办法是在动态生成的html标签中使用onclick="xxxxxxxx()"这种形式 好处是不像live那样耗费浏览器资源
$(".LeftTask").click(function(){
改成
$(".LeftTask").live("click",function(){ .......
后者的优势是可以"即使某dom节点是页面载入完成之后生成的,也能绑定事件".
参阅jquery手册.
出错信息:
Line: 408
Error: Object doesn't support property or method 'live'
可能是我在head里面设置的版本问题。
前两天你给我的checkbox中的attr也出错了,最后换成最新版本中prop,才最终成功。
中间多了个空格!
变成了
不出问题才怪呢
中间多了个空格!
变成了
不出问题才怪呢
版主英明,刚才我自己查了很久,也同时发现这个问题。
这样,版主指明问题,再看我一眼一贯支持。
结贴,两位都是熟人,你看这样可否:
版主65分,再看我一眼35分如何?
这样,版主指明问题,再看我一眼一贯支持。
结贴,两位都是熟人,你看这样可否:
版主65分,再看我一眼35分如何?
都是熟人,就暂且这样。有觉得不满意的,可回帖或私信给我。
一再感谢大牛们帮助!!!
分数都是浮云,无所谓的.... 有问题继续交流.... 版大确实犀利 一眼看出问题所在~

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools
