如何使用javascript设置下拉列表的默认选择索引值 [英] How do i set default selected index value of drop down list using javascript
本文介绍了如何使用javascript设置下拉列表的默认选择索引值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有公司下拉列表。我想将公司名称设为default.i通过传递公司名称ID从数据库中检索该值。
我有使用了这段代码,但它不能正常工作
我尝试了什么:
var sel = document.getElementById('Company_ID');
sel.selectedIndex = 1;
I have company drop down list.i want to make company name as default.i retrieve that value from database through passing id of company name.
I have used this code,but it's not working
What I have tried:
var sel = document.getElementById('Company_ID');
sel.selectedIndex = 1;
推荐答案
不相信,我测试过你编码。正如我预期的那样,它起作用了。
结论:错误发生在其他地方,而不是你展示的代码。例如,您要使用的select
元素可能没有id =Company_ID
属性。另一种可能性是:某些其他JavaScript代码错误或甚至词汇错误,因此您的代码片段永远不会被执行。我在代码中遇到词汇错误或异常,脚本将无声地失败,除非你采取特殊措施来揭示问题。
建议:使用JavaScript调试器。您将立即找到问题。
-SA
In disbelieve, I tested you code. Exactly as I expected, it worked.
Conclusion: the bug is elsewhere, not in the code you show. For example, theselect
element you want to use might not have the attributeid="Company_ID"
. Another possibility is: some other JavaScript code is wrong or even lexically incorrect, so your fragment of code is never executed. I case of lexical bug or exception in code, the script will fail silently, unless you take special measures to reveal the problem.
Advice: use JavaScript debugger. You will locate the problem in no time.
—SA
这篇关于如何使用javascript设置下拉列表的默认选择索引值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文