2013-10-10 5 views
0

폼 기반 메뉴가있어서 선택 옵션에 기본 nav UL 컨텐츠가 자동으로 채워집니다. 그것은 훌륭하게 메뉴의 위치를 ​​반영하는 대시를 증가시킵니다.JQuery에 동적으로 채워진 HREF 추가하기 선택

그러나 UL에있는 링크의 HREF는 채우지 않습니다.

는 내가 가지고있는 코드 :

<script> 
     $(function() { 
       var options = '<option selected></option>'; 

       $('#primary-nav').find('a').each(function() { 
        var text = $(this).text(), 
        depth = $(this).parent().parents('ul').length, 
        depthChar = '', 
        i = 1; 
        for (i; i < depth; i++) { depthChar += '&ndash;&nbsp;'; 
         } 
        options += '<option>' + depthChar + text + '</option>'; 
       }); 
       $('<select id=\'mobile-menu\' />').append(options).appendTo('#mobile-nav'); 

       $("#primary-nav select").change(function() { 
        window.location = $(this).find("option:selected").val(); 
       }); 

      }); 


     </script> 

어떻게 든 아래를 추가 할 필요를 위의 클릭하면 선택한 옵션이 표시된 값을 기준으로 URL을 URL로 이동, 그리고 그 때문에 ;

$("#primary-nav a").each(function() { 
var el = $(this); 
$("<option />", { 
"value" : el.attr("href"), 
"text" : el.text() 
}).appendTo("#primary-nav select"); 
}); 

누구에게 어떻게 알림을 제공 할 수 있습니까?

감사합니다.

답변

0

당신은 당신의 현재 코드의 값 속성을 추가 할 수 있습니다

$('#primary-nav').find('a').each(function() { 
    var text = $(this).text(), 
     href = $(this).attr('href'), 
     depth = $(this).parent().parents('ul').length, 
     depthChar = '', 
     i = 1; 
    for (i; i < depth; i++) { 
     depthChar += '&ndash;&nbsp;'; 
    } 
    options += '<option value="'+href+'">' + depthChar + text + '</option>'; 
}); 
$('<select id=\'mobile-menu\' />').append(options).appendTo('#mobile-nav'); 
관련 문제