我正在开发一个jquery下拉菜单。(演示here)
我需要做的是获得用户选择的完整路径,并验证右边没有更多的选项。最初的想法(在下拉菜单之前,只有普通的li/ul)是通过这个fiddle实现的
如您所见,这里有验证和一个完整的path变量供以后使用。
现在,我想要实现的是下拉菜单的相同功能。
这是一段代码,我在其中根据下拉菜单使用用户的选择填充标签。
<asp:Label ID="LabelMenu" text="" runat="server" /> <span id="menuSelection"></span>如您所见,得到结果的是span id menuSelection。
所以我的问题是。对于menuSelection结果,我如何替换小提琴上显示的click函数?
目标是在用户从下拉菜单中选择一个字段后获得完整路径和验证
致以问候。
发布于 2012-10-17 07:25:25
您可以使用change事件代替单击
$("select").on('change' , function() {发布于 2012-11-05 22:16:32
我可能会误解你,但这至少会给你一个想法。
资源:
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>html:
<label id="LabelMenu" text="" runat="server" />
<span id="menuSelection">My Span List</span>jQuery:
$(document).ready(function(){
// Flag to prevent multiple insertion
var flag = false;
$("#menuSelection").click(function() {
// Prevent inserting more than 1 menu
if (flag == false) {
// Your menu
$('<ul id="mylist"> <li><a href="#">First Level 1</a> <ul> <li><a href="#">FL1 Second Level 1</a></li> <li><a href="#">FL1 Second Level 2</a></li> </ul> </li> <li><a href="#">First Level 2</a> <ul> <li><a href="#">FL2 Second Level 1</a> <ul> <li><a href="#">FL2 SL1 Third Level 1</a></li> </ul> </li> <li><a href="#">FL2 Second Level 2</a> <ul> <li><a href="#">FL2 SL2 Third Level 1</a></li> </ul> </li> <li><a href="#">FL2 Second Level 3</a></li> </ul> </li> <li><a href="#">First Level 3</a> <ul> <li><a href="#">FL3 Second Level 1</a> <ul> <li><a href="#">FL3 SL1 Third Level 1</a></li> <li><a href="#">FL3 SL1 Third Level 2</a></li> </ul> </li> <li><a href="#">FL3 Second Level 2</a></li> <li><a href="#">FL3 Second Level 3</a> <ul> <li><a href="#">FL3 SL3 Third Level 1</a></li> </ul> </li> </ul> </li> <li><a href="#">First Level 4</a> </li> </ul>'
).insertAfter($(this)).menu().focus().focusout(function() {
// When out of menu remove this instance
$(this).empty().remove();
// Flag false again
flag = false;
});
// Find which menu item is clicked
$('#mylist a').click(function() {
var route=$(this).parent().text();
if($(this).next().length==0){
alert('no more levels');
alert(route);
$('#LabelMenu').text(route);
}else{
alert(route);
$('#LabelMenu').text($(this).text());
}
});
}
// Change flag on click
flag = true;
});
});发布于 2012-11-01 00:26:14
$('element.selector').on('click', function(event) {
event.preventDefault();
// your logic
});https://stackoverflow.com/questions/12924938
复制相似问题