在下拉列表中将表单选项设置为默认值 [英] set up form option as default in a drop down

查看:309
本文介绍了在下拉列表中将表单选项设置为默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上创建了一个驱动数据库下拉菜单。它将用户选择的值发送到数据库进行检查并返回一个特定的项目。

现在一切正常,但我试图预先选择第一个值以便用户将第一个选项作为默认选项,并且可以直接将其添加到购物车,并且他们还可以选择不同的尺寸。到目前为止,我在下拉列表中的选项中尝试了选择,但它没有做任何事情,只是在页面加载时显示值。



最后一件事是我提到,我使用Ajax函数来提交选定的值并从数据库返回结果。

解决方案

我可以想到两种方法为了这。


  1. 请求页面时,请在服务器端代码中调用数据库以预先存储该值。

  2. 使用javascript的 onload 事件选择值作为部分的客户端脚本。您可以使用这个答案作为参考。


I have created a driven database drop down menu on my website. It sends the selected value by the user to the database to be checked and returns a specific item.

Everything works perfectly now but I'm trying to pre select the first value so that the user will have the first option as default and can add it to cart directly and they also can choose a different size. So far, I tried 'selected' inside the option in the drop down but it doesn't do anything but displays the value when the page loads.

One last thing to mention, I'm using Ajax function to submit selected value and return result from the database.

解决方案

I can think of two ways for this.

  1. When the page is requested, make a call to your DB in your server-side code to store that value beforehand.
  2. Use javascript's onload event to select the value as a part of a client-side script. You can use this answer as reference.

这篇关于在下拉列表中将表单选项设置为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆