当我单击一个选项时,如何使该选项在 JQUery 中选中?
你好, 我有一个选择下拉菜单,例如
<select id="listForms">
<option value="Personal Form" id="25">Personal Form</option>
<option value="Employee Details Form" id="24">Employee Details Form</option>
<option value="Contact Form" id="45">Contact Form</option>
</select>
当我单击一个选项时,如何使该选项在 JQUery 中被选中?
编辑:
我已经尝试过
$("#listForms option").click(function (){
$("#listForms option").attr("selected",true); but not working
});
如何找到稍后选择的那个?在 JQuery 中??请建议我..
HI,
i am having a select dropdown like
<select id="listForms">
<option value="Personal Form" id="25">Personal Form</option>
<option value="Employee Details Form" id="24">Employee Details Form</option>
<option value="Contact Form" id="45">Contact Form</option>
</select>
When i click on a option how to make that option as selected in JQUery??
Edit :
i have tried it with
$("#listForms option").click(function (){
$("#listForms option").attr("selected",true); but not working
});
also how to find which one has been selected later?? in JQuery ?? please suggest me..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将获得选定的选项。
will get the selected option.
使用以下 HTML 下面
的 jQuery 会告诉弹出一个带有您指定的值的警报框:
例如,当您单击“个人表单”时,您会看到一个显示“个人表单”的警报窗口。
如果您希望在选择时弹出“25”,您可以将
$(this).val()
更改为$(this).attr('id')
“个人表格”选项。With the following HTML
The following jQuery would tell pop up an alert box with the value you've specified:
For example, when you click "Personal Form", you'd see an alert window that says "Personal Form".
You could change
$(this).val()
to$(this).attr('id')
if you wanted "25" to pop up when you choose the "Personal Form" option.也许这就是您所需要的:http://jgeewax.wordpress。 com/2008/09/01/jquery-dropdown-fun/
编辑 - 使用上述站点的道具:
这是他给出的最终解决方案:
使用此 html 片段:
Maybe this is what you need: http://jgeewax.wordpress.com/2008/09/01/jquery-dropdown-fun/
EDIT - with props to the site mentioned above:
This is the final solution he gives:
With this html snippet:
我猜你想要选项中的值和 id?...试试这个:
I'm guessing you want the vaule and id from the options?... try this:
这是我的代码片段,我遇到的问题是在我选择一个选项并且页面 url 发生更改并向 URL 附加一个值后,我选择的值未设置为我选择的值,而是返回到“选择”默认值。
This is my snippet code here The problem i have is after i select an option and the pages url changes and appends a value to the URL the value i selected isn't set to what i selected but goes back to "select" the default value.