2014-09-06 198 views
0

當我想從第一個下拉菜單中選擇一個選項時,它也會在第二個下拉菜單中選擇選項。選擇選項無法正常工作

*需要單獨下拉菜單。

*需要從下拉菜單中點擊文字時選擇單選按鈕。

這裏是一個jsfiddle

回答

0

你的問題的問題是,你的兩個下拉菜單中有相同的類,這是你的第一個問題,以便代替類的原因嘗試使用的ID。

回答第一個問題(需要單獨從下拉式菜單選項)在小提琴所示: - http://jsfiddle.net/sbact4hz/

的第二個問題的答案(需要選擇單選按鈕,當點擊從下拉菜單中的文字): -

$('.search-panel .dropdown-menu').find('a').click(function() { 
    var param = $(this).attr("href").replace("#",""); 
    var concept = $(this).text(); 
    $('.search-panel span#search_concept').text(concept); 
    $('.input-group #search_param').val(param); 
    $(this).find('input[type=radio]').prop('checked',true); //OR $(this).find('input[type=radio]').attr('checked','checked'); <----- add this line 
}); 
+0

我真的擺脫道具行註釋的。使用'prop'比'attr'好得多! – ThiefMaster 2014-09-06 07:58:09

+0

@ThiefMaster ...是的,但我在這裏給出兩種方式,提問者可以選擇其中的任何一種。 – 2014-09-06 07:59:52

+0

儘管第二種方式沒有任何優勢。 – ThiefMaster 2014-09-06 08:00:15

0

您必須向上遍歷,因爲您的點擊事件處於超鏈接深處。

你的答案與下拉菜單,並選擇電臺獨立工作是

$('.search-panel .dropdown-menu').find('a').click(function() { 
    var param = $(this).attr("href").replace("#",""); 
    var concept = $(this).text(); 
    $(this).parent().parent().parent().find('button > span#search_concept').text(concept); 
    $(this).find('input').prop('checked', true); 
    $('.input-group #search_param').val(param); 
}); 
+0

是的..我們可以像你一樣使用'parent()'顯示在你的答案,但我個人不喜歡這種方法使用'parent()',因爲那麼它將停止工作,如果我們添加另一個標籤或父母的下拉離開其他下拉,因爲它是.. – 2014-09-06 10:03:05

+0

@Telmo席爾瓦看到這個問題並回答它。 http://stackoverflow.com/questions/25699409/select-option-doesnt-work-separately – sohag513 2014-09-06 10:42:47

相關問題