简体   繁体   English

在下拉菜单中将表单选项设置为默认选项

[英]set up form option as default in a drop down

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. 最后要提到的是,我正在使用Ajax函数来提交选定的值并从数据库返回结果。

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. 使用javascript的onload事件选择该值作为客户端脚本的一部分。 You can use this answer as reference. 您可以将此答案用作参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM