选择项目时更改下拉菜单的文字

时间:2015-05-16 05:53:34

标签: javascript jquery twitter-bootstrap jquery-ui twitter-bootstrap-3

根据下拉列表的选择,文本必须在rails应用程序中更改。我经历了How to change the main display of dropdown when an item is selected in bootstrap我添加了js并尝试集成到我的应用程序中但它无法正常工作。



%hearder
	%nav.navbar.navbar-job
		.container{class: "text-center"}
			%div{class: "btn-group"}
				%ul.dropdown
					I am 
					%a.btn.dropdown-toggle.btn-job{"data-toggle" => "dropdown","role" => "button"}
						Select 
						%span{class: "caret",id: "dropdown_title"}
					%ul.dropdown-menu{id: "divNewNotifications"}
						%li= link_to "All Jobs", jobs_path
						- Jobcategory.all.each do |jobcategory|
							%li= link_to jobcategory.name, jobs_path(jobcategory: jobcategory.name)






$('.dropdown-toggle').dropdown();
$('#divNewNotifications li').on('click', function() {
    $('#dropdown_title').html($(this).find('a').html());
    });




更新:



%hearder
	%nav.navbar.navbar-job
		.container{class: "text-center"}
			%div{class: "btn-group"}
				%ul.dropdown
					%a.btn.dropdown-toggle.btn-job{"data-toggle" => "dropdown","role" => "button"}
						Select
						%span{class: "caret",id: "dropdown_title"}
					%ul.dropdown-menu{id: "divNewNotifications"}
						%li= link_to "All Creative Jobs", jobs_path
						- Jobcategory.all.each do |jobcategory|
							%li= link_to jobcategory.name, jobs_path(jobcategory: jobcategory.name)
					Designer
  
								
%body
	%div{id:"testing"}
		= render 'testing'
		






dropdown.js

$('#divNewNotifications li').on('click', function() {
    $('.dropdown-toggle').html($(this).find('a').html());
});


$(function(){
	$("#divNewNotifications li").live('click', function(){
		$.get(this.href,null,null,"script");
		return false;
	});
});






index.js.erb

$("#testing").html("<%= escape_javascript(render("testing")) %>");
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我想你想做这样的事情......

$('#divNewNotifications li').on('click', function() {
    $('.dropdown-toggle').html($(this).find('a').html());
});

http://www.bootply.com/9KeT6fk2PM

您不需要使用$('.dropdown-toggle').dropdown();,因为按钮上使用了数据切换属性。