通过数据库条目设置下拉菜单默认值
我已经四处寻找这个问题,但找不到直接的答案,所以就这样吧。 我有一个包含一些选项的下拉列表 - 通过 HTML 填充,一旦选择了一个选项并且用户提交了表单,他们选择的选项的值就会插入到数据库中。当他们返回下拉列表时,我的问题出现了,他们显示的是顶部的选项,而不是他们之前接受的选项。我如何将用户选择的选项设置为下拉列表中的默认选项? (欢迎使用 Javascript 和 jQuery)
I've searched around for this, but cannot find a direct answer so here it goes.
I have a drop down list with some options - populated via HTML, once an option is selected and the user submits the form the value of the option they've selected is inserted into the database. My problem arises when they then return to the drop down list, they are displayed the option that is at the top and not the one they previously accepted. How would I go about setting the one selected by the user as the default option in the drop down list?
(Javascript and jQuery are welcome)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的方法是使用服务器端脚本语言修改刚刚插入数据库的选项的“选定”属性。
The best approach would be to modify the "selected" attribute of the option what was just inserted into the database, using your server-side scripting language.
您可以使用 Ajax 提交表单,并让服务器返回“已成功添加到数据库”消息。由于整个页面未刷新,因此该选项应该已保持选中状态。如果没有,只需使用简单的 JavaScript 添加“selected”属性,就像 Emil 的回答一样。
You could submit the form with Ajax, and have the server return a "successfully added to database" message. Since the whole page isn't refreshed, the option should already remain selected. If not, it's simple JavaScript to add the 'selected' attribute like in Emil's answer.