简体   繁体   中英

jquery ul li menu problem

I am working on dynamic creating drop down menus like:

<dl id="sample-1" class="dropdown">
    <dt id="1"><a href="#" id="1"><span>Please select the country</span></a></dt>
    <dd>
        <ul>
            <li><anchortag>Brazil fdsf sdf kjdsh  hkjh jkh hdf sdh kjh sdfh  kjh k hkjh 2121 fgfd21g 21 21g dfg1 f21gfd2<span class="value">BR</span></ahchor></li>
            <li><anchortag>France<span class="value">FR</span></ahchor></li>
            <li><anchortag>Germany<span class="value">DE</span></ahchor></li>
            <li><anchortag>India<span class="value">IN</span></ahchor></li>
            <li><anchortag>Japan<span class="value">JP</span></ahchor></li>
            <li><anchortag>Serbia<span class="value">CS</span></ahchor></li>
            <li><anchortag>United Kingdom<span class="value">UK</span></ahchor></li>
            <li><anchortag>United States<span class="value">US</span></ahchor></li>
        </ul>
    </dd>
</dl>
<span id="result-1"></span>

using javascript code:

< script > $('dl[id^="sample-"] dt a').click(function(e) {
    e.preventDefault();
    //$('dl[id^="sample-"] dd ul').toggle();                      $(this).closest('dt').siblings('dd').find('ul').toggle();
});

$('dl[id^="sample-"]').each(function() {
    var mainOjbect = this;
    //$(this.closest('dt').find('a')).live('click', function(e) {                                          
    $("dl[id^='sample-'] dd ul li a").live('click', function(e) {
        var text = $(this).html();
        alert(text);
        alert(mainOjbect.id + "id is....");
        $('#' + mainOjbect.id + " dt a span").html(text);
        //$(this).closest('dt').find('a').find('span').html(text);
        //$("dl[id^='sample-'] dd ul").hide();              
        $('#' + mainObject + "ul").hide();
        $(this).find('dl[id^="result-"]').html("Selected value is: " + getSelectedValue(this));
    });
});

function getSelectedValue(id) {
    alert(id);
    return $("#" + id).find("dt a span.value").html();
}

$(document).bind('click', function(e) {
    alert('hello3');
    var $clicked = $(e.target);
    if (!$clicked.parents().hasClass("dropdown"))
    //$("dl[id^='sample-'] dd ul").hide();
    $(this).closest('dt').siblings('dd').find('ul').hide();
}); < /script>

These all menu are being created dynamically using javascript clone function and the next will be

<dl id="sample-2" class="dropdown">
...
...
...
<dl id="sample-n" class="dropdown">

but I am not getting understand when I click on one menu the selection is being done on the other menu, I want to run all these menus in their places. I have spent enough time but not getting understand where I have wrong code. Need help how to traverse all these.

Thanks

as i have interpretted ur question to traverse all the menu items upon click u can do something like below...

 $('.dropdown').live('click',function(){


    $('li').each(function(){

    var $idOfli=$(this).attr('id');
    .
    .
    .
    }

    });

Here is a sample drop down that allows you to select. Not certain if it is exactly what you are looking for. Hopefully it gets you started.

http://jsfiddle.net/rcravens/HCqHG/

Bob

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM