鉴于 RoR (Ruby 1.9) 应用程序,我有一个组合框,如下所示:
<%= collection_select(:source, :source_id, @objects, :id, :name, :selected =>1)
我想在 javascript 中更改选定的值。有可能吗?如果是,请向我展示有关此的示例代码。
谢谢。
鉴于 RoR (Ruby 1.9) 应用程序,我有一个组合框,如下所示:
<%= collection_select(:source, :source_id, @objects, :id, :name, :selected =>1)
我想在 javascript 中更改选定的值。有可能吗?如果是,请向我展示有关此的示例代码。
谢谢。
您可以使用以下内容更改选择框的选定选项:
var sel = document.querySelectorAll('select[name="test"]')[0]; // Change this to target your select box
sel.selectedIndex = 1;
如果您愿意使用 JQuery,它非常容易
<%= collection_select(:source, :source_id, @objects, :id, :name, html_options = {selected: 1, id: 'yourid'})
在 .js 中
$("#yourid").val('thevalueyouwantselected');
希望有帮助