在下拉列表中自动选择正确的项目(Automatically selecting the correct item in dropdown list)

我正在开发一个网站的应用程序,我想知道是否有一种方法可以自动设置下拉列表中的值,以便根据MySQL数据库中的值自动选择? 我确定有,但我可以决定从哪里开始。 例如,如果一个人在网站上有一个存储他们的姓名,地址等的个人资料,并且他们去更新它,我希望下拉列表自动设置他们最初存储在数据库中的任何内容,以便自动选择。

我在尝试这个:

<script type="text/javascript"> $("#state option[value='CA']").attr("selected", "selected"); </script> <select id="state" name="state"> <option value="">Select One</option> <option value="AK">Alaska</option> <option value="AL">Alabama</option> <option value="AR">Arkansas</option> <option value="AZ">Arizona</option> <option value="CA">California</option> Etc….

但我似乎无法让它发挥作用。 一旦我确实工作,我想将其更改为:

$("#state option[value='<?=$state?>']").attr("selected", "selected");

有什么建议么? jQuery甚至是在这里使用的合适工具,还是有更好的选择?

谢谢!

I was working on an application for a website, and I was wondering if there is a way to automatically set a value in a dropdown list to be selected automatically based on a value from a MySQL database? I'm sure there is, but I can;t decide where to start. For example, if a person has a profile on a website that stores their name, address, etc., and they go to update it, I want the dropdown list to automatically set whatever they had originally stored in the database to be selected automatically.

I was trying this:

<script type="text/javascript"> $("#state option[value='CA']").attr("selected", "selected"); </script> <select id="state" name="state"> <option value="">Select One</option> <option value="AK">Alaska</option> <option value="AL">Alabama</option> <option value="AR">Arkansas</option> <option value="AZ">Arizona</option> <option value="CA">California</option> Etc….

But I can’t seem to get it to work. Once I do get it working, I would like to change it to:

$("#state option[value='<?=$state?>']").attr("selected", "selected");

Any suggestions? Is jQuery even the right tool to use here, or is there a better alternative?

Thanks!

最满意答案

您必须使用文档加载事件,因为执行脚本时尚未创建选择列表:

<script type="text/javascript"> $(function() { $("#state option[value='CA']").attr("selected", "selected"); }); </script> <select id="state" name="state"> <option value="">Select One</option> <option value="AK">Alaska</option> <option value="AL">Alabama</option> <option value="AR">Arkansas</option> <option value="AZ">Arizona</option> <option value="CA">California</option>

jQuery甚至是在这里使用的合适工具,还是有更好的选择?

imho它是正确的工具,因为与服务器端if语句相比,它使视图更清晰。

You have to use the document load event since the select list have not been created when the script is executed:

<script type="text/javascript"> $(function() { $("#state option[value='CA']").attr("selected", "selected"); }); </script> <select id="state" name="state"> <option value="">Select One</option> <option value="AK">Alaska</option> <option value="AL">Alabama</option> <option value="AR">Arkansas</option> <option value="AZ">Arizona</option> <option value="CA">California</option>

Is jQuery even the right tool to use here, or is there a better alternative?

imho it's the right tool since it makes the view cleaner compared to a server side if statement for every row.

更多推荐