Javascript 单击时重置引导下拉按钮值

Javascript 单击时重置引导下拉按钮值,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,我使用引导下拉组件来代替普通的选择菜单。当用户在下拉列表中选择一个选项时,它将显示在下拉按钮上。这很好,但我需要允许用户使用onclick函数将按钮重置回默认值 HTML: 这说明了这个问题 正如您所见,单击“清除”按钮时,它会删除引导下拉按钮中的所有内容,但这是错误的,因为我需要将页面重新设置为默认值,就像页面已刷新一样?您应该将默认值存储在变量中,并将其插入重置函数。您可以使用清除按钮本身,将下拉列表的默认值保存为按钮的属性 在dom就绪时,您可以保存这样一个值,并用一段代码处理click事

我使用引导下拉组件来代替普通的选择菜单。当用户在下拉列表中选择一个选项时,它将显示在下拉按钮上。这很好,但我需要允许用户使用onclick函数将按钮重置回默认值

HTML:

这说明了这个问题


正如您所见,单击“清除”按钮时,它会删除引导下拉按钮中的所有内容,但这是错误的,因为我需要将页面重新设置为默认值,就像页面已刷新一样?

您应该将默认值存储在变量中,并将其插入重置函数。

您可以使用清除按钮本身,将下拉列表的默认值保存为按钮的属性

在dom就绪时,您可以保存这样一个值,并用一段代码处理click事件:

$('.resetdropdown').on('click', function(e) {
    //
    // if the attribute doesn't exist add it...
    //
    if ($(this).attr('defValue') == undefined) {
        $(this).attr('defValue', ' ' + $('.dropdown-toggle')
              .contents().get(2).textContent + ' ');
    } else {
        //
        // ...else do the click event stuff
        //
        $('.dropdown-toggle').contents().get(2).textContent = 
                 ' ' + $(this).attr('defValue') + ' ';
    }
}).trigger('click');  // trigger the click immediately after event handler declaration.
片段:

$'divNewNotifications li'。单击,函数E{ $'.dropdown toggle'.contents.filterfunctionidx,ele{ 返回ele.nodeType==Node.TEXT\u Node&&ele.textContent.trim.length!=0 }.get0.textContent=''+此.textContent.trim+''; }; $'.resetdropdown'。单击,函数E{ 如果$this.attr'defValue'==未定义{ $this.attr'defValue','+$'.dropdown toggle'.contents.get2.textContent+''; }否则{ $'.dropdown toggle'.contents.get2.textContent='+$this.attr'defValue'+''; } }.触发“点击”; 选择 一 二 三 清楚的
嗨,这真的很有帮助,你能帮我怎么重置这种格式的下拉列表吗?多谢各位@gaetanoM@monisogani更新后的提琴会让我知道它是否有效。@gaetanoM:对不起,我有更改请求,当一页上有多个下拉菜单,只有一个清除按钮来重置所有下拉菜单时,我如何实现这一点?这是最新的小提琴-
$('#divNewNotifications li').on('click', function() {
$('.dropdown-toggle').html($(this).find('a').html());
});


$('.resetdropdown').on('click', function() {
$('.dropdown-toggle').html(" ");
});
$('.resetdropdown').on('click', function(e) {
    //
    // if the attribute doesn't exist add it...
    //
    if ($(this).attr('defValue') == undefined) {
        $(this).attr('defValue', ' ' + $('.dropdown-toggle')
              .contents().get(2).textContent + ' ');
    } else {
        //
        // ...else do the click event stuff
        //
        $('.dropdown-toggle').contents().get(2).textContent = 
                 ' ' + $(this).attr('defValue') + ' ';
    }
}).trigger('click');  // trigger the click immediately after event handler declaration.