2014-02-16 65 views
0

更新問題:現在我可以讓白色襯衫工作,但不是黃色。我可以在白色的小med-lrg之間切換,但換到黃色仍然只會在白色襯衫大小之間變化。我已經凝結代碼,只顯示小小白和小黃如果選中單選按鈕並選擇了值,jquery

<select class="color"> 
    <option value="white">White</option> 
    <option value="yellow">Yellow</option> 
</select> 
    <input type="radio" class="size" name="size" value="small">Small 
    <input type="radio" class="size" name="size" value="medium" checked>Medium 
    <input type="radio" class="size" name="size" value="large">Large 

和JS:

$('.size').click(function() { 
    shirtButtons.hide(); 
    if($('.color').val() == 'white' || $('.size:checked').val() == 'small'){ 
     shirtButtons.hide(); 
     $('.white-small').show(); 
    }else if($('.color').val() == 'yellow' || $('.size:checked').val() == 'medium'){ 
     shirtButtons.hide(); 
     $('.yellow-medium').show(); 
    } 
}); 
$('.color').change(function() { 
    shirtButtons.hide(); 
    if($('.color').val() == 'white' || $('.size:checked').val() == 'small'){ 
     shirtButtons.hide(); 
     $('.white-small').show(); 
    }else if($('.color').val() == 'yellow' || $('.size:checked').val() == 'small'){ 
     shirtButtons.hide(); 
     $('.yellow-small').show(); 
    } 
}); 
+1

你忘了js嗎? –

+0

對不起!添加! – Astarkey

+0

@Astarkey什麼是shirtButtons? – Pavlo

回答

1

下面是一些例子,我已經爲你創建:
HTML代碼

<select class="color"> 
    <option value="white">White</option> 
    <option value="yellow">Yellow</option> 
</select> 

<input type="radio" class="size" name="size" value="small">Small 
<input type="radio" class="size" name="size" value="medium" checked>Medium 
<input type="radio" class="size" name="size" value="large">Large 

JS代碼

$('.size').click(function() { 
    myFunction(); 
}); 

$('select.color').change(function() { 
    myFunction(); 
}); 

function myFunction(){ 
    shirtButtons.hide(); // Don't know what is shirtButtons, please explain 
    var color = $('select :selected').val(); 
    var size = $('.size:checked').val(); 

    if(color == 'white' || size == 'small'){ 
    shirtButtons.hide(); // Don't know what is shirtButtons, please explain 
    $('.white-small').show(); 
    } else if(colort == 'yellow' || size == 'medium'){ 
     shirtButtons.hide(); // Don't know what is shirtButtons, please explain 
     $('.yellow-medium').show(); 
    } 
} 

所以,你可以嘗試alertconsole.log變量colorsize
請讓我知道你有什麼問題。

2

你必須一類size添加到輸入。

<input type="radio" class="size" name="size" value="small">Small 
<input type="radio" class="size" name="size" value="medium" checked>Medium 
<input type="radio" class="size" name="size" value="large">Large 

然後使用:checked選擇器獲得的值:

$(".size:checked").val() 

全部JS

$('.color, .size').click(function() { 
    if($('.color').val() == 'white' || $(".size:checked").val() == 'small'){ 
     shirtButtons.hide(); 
     $('.white-small').show(); 
    }else if($('.color').val() == 'white' || $(".size:checked").val() == 'medium'){ 
     shirtButtons.hide(); 
     $('.white-medium').show(); 
    } //etc 
}); 

JS小提琴:http://jsfiddle.net/B93WW/

+1

@Astarkey很高興我能幫到你! –

+1

我不認爲點擊「.color」選擇的事件是正確的。你應該使用$('select')。change()而不是 – Pavlo

+0

@Pavlo糾正我,如果我錯了,但我很確定'click'是'input'上的一個事件。 –