2013-03-21 28 views
0

我試圖用這個在我的jQuery的,這樣我可以使用相同的類,但有相互獨立如果選擇了值,那麼做到這一點

繼承人的撥弄我的影響在

http://jsfiddle.net/d0okie0612/ThnKc/

工作,你可以當您選擇選項3字HEY出來兩個選擇標籤,但我想它使用相同的類是相互獨立的。

我想我能做到這一點使用類似

$(this).children('div.custom_size').fadeIn() 

,但它似乎沒有工作,我一直在堅持和沮喪了很久

繼承人的HTML

<select id="print_size_options_LargeFormatBlackWhite"> 
    <option value="">1</option> 
    <option value="">2</option> 
    <option value="customSize">3</option> 
</select> 

<div class="custom_size" style="display: none;"> 
    Hello 
</div> 

<br /> 
<br /> 
<br /> 
<select id="print_size_options_LargeFormatBlackWhite"> 
    <option value="">1</option> 
    <option value="">2</option> 
    <option value="customSize">3</option> 
</select> 

<div class="custom_size" style="display: none;"> 
    Hello 
</div> 

繼承人Jquery

$("#print_size_options_LargeFormatBlackWhite").change(function(evt) { 
    var selected; 
    selected = $(this).val(); 
    if (selected === "customSize") { 
     return $(".custom_size").fadeIn(); 
    } else { 
    return $(".custom_size").fadeOut(); 
    } 
    }).change(); 

我是jQuery的超級新手,所以如果你能在小提琴中表現出我會很棒!

謝謝!

回答

1

這可能會有所幫助。 ID我認爲應該爲元素

http://jsfiddle.net/sUuqd/1/

$(".print_size_options_LargeFormatBlackWhite").change(function(evt) { 
var selected; 
selected = $(this).val(); 
if (selected === "customSize") { 
    return $(this).parent().find(".custom_size").fadeIn(); 
    } else { 
return $(this).parent().find(".custom_size").fadeOut(); 
} 
}).change(); 
+0

不,「id」不應該是唯一的,它必須在文檔中是唯一的。 – 2013-03-21 22:15:40

+0

你說得對。謝謝 – Damian0o 2013-03-21 22:17:36

0

供應相對的上下文到$(this)元件:

if (selected === "customSize") { 
    return $(this).next(".custom_size").fadeIn(); 
    } else { 
    return $(this).next(".custom_size").fadeOut(); 
    } 

JS Fiddle demo

此外你有無效的HTML,一個id必須是文檔中是唯一的,因此,你應該使用一個類名稱爲您select元素代替:

<select class="print_size_options_LargeFormatBlackWhite"> 
<!-- removed for brevity --> 
</select> 
<div class="custom_size" style="display: none;">Hello</div> 
<!-- removed for brevity --> 
<select class="print_size_options_LargeFormatBlackWhite"> 
<!-- removed for brevity --> 
</select> 
<div class="custom_size" style="display: none;">Hello</div> 

隨着jQuery的:

$(".print_size_options_LargeFormatBlackWhite").change(function (evt) { 
    var selected = $(this).val(); 
    $(this).next('.custom_size')[selected === 'customSize' ? 'fadeIn' : 'fadeOut'](); 
}).change(); 

JS Fiddle demo

最後,因爲有時我只是想有點太多了使用三元運算符:

$(".print_size_options_LargeFormatBlackWhite").change(function (evt) { 
    var selected = $(this).val(); 
    $(this).next('.custom_size')[selected === 'customSize' ? 'fadeIn' : 'fadeOut'](); 
}).change(); 

JS Fiddle demo

+0

其他輸入標籤似乎並沒有與該代碼一起工作,只有頂部的顯示'嘿' – user1502223 2013-03-21 22:10:31

+0

是的,那是因爲你有*兩個*元素共享一個無效的'id'。請參閱您在評論時所做的修改。 – 2013-03-21 22:14:03

+0

@ user1502223看看我的答案,尤其是小提琴。這表明你在找什麼。還注意到我將選擇器從id更改爲「select」 – ITroubs 2013-03-21 22:20:06

0

看看是唯一我fiddle。我在select和us屬性中添加了for="someid"屬性來標識選擇代表哪個div。

我的JS是這樣的:

$("select").change(function(evt) { 
    var selected; 
    selected = $(this).val(); 
    if (selected === "customSize") { 
    return $(".custom_size#"+$(this).attr("for")).fadeIn(); 
    } else { 
    return $(".custom_size#"+$(this).attr("for")).fadeOut(); 
    } 
}).change(); 

和HTML這樣的:

<select id="print_size_options_LargeFormatBlackWhite" for="a"> 
    <option value="">1</option> 
    <option value="">2</option> 
    <option value="customSize">3</option> 
</select> 

<div id="a" class="custom_size" style="display: none;"> 
    Hello 
</div> 

第二選擇只是有一個for="b"和第二div有id="b"

+0

'for'屬性對'select'元素無效,它只對'label'元素有效(將'label'與'input'(或'select'或'textarea')關聯起來。如果你必須使用自定義屬性,那麼使用'data- *'屬性,那麼至少它是有效的(在HTML 5下)。 – 2013-03-21 22:22:07

+0

好吧,你也可以用rel或者其他東西替換它... – ITroubs 2013-03-21 22:23:59

+0

嗯,是的。我的觀點並不是「數據*」是* only *選項,但如果你打算使用非標準元素,我建議不要使用對該元素特別無效的屬性(不管是什麼其他屬性,你選擇,而不是使用)。 – 2013-03-21 22:25:43

相關問題