Текст ярлыка должен обновляться в зависимости от того, что выбрано в параметрах выбора. Что я делаю не так?
HTML:
<label id="amount" name="amount">Test</label>
<select name="cycle" id="cycle">
<option>Monthly</option>
<option>Annually</option>
</select>
JQuery:
$(document).ready(
function () {
$('#cycle').change(
function () {
var rate = 0;
if (('#cycle option:selected'.text() == "Monthly")) {
rate = 2;
} else {
rate = 5;
}
$('#amount').text('$' + rate);
});
});
Есть несколько проблем
Пытаться
$(document).ready(function () {
$('#cycle').change(function () {
var rate = 0;
//check the value of the select element
//missing $ before your selector
if ($(this).val() == "Monthly") {
rate = 2;
} else {
rate = 5;
}
$('#amount').text('$' + rate);
});
});
Демо: скрипка
Вы забыли включить jQuery в свою скрипку, но фактическая ошибка была в строке:
if (('#cycle option:selected'.text() == "Monthly")) {
должен быть:
if ($('#cycle option:selected').text() == "Monthly") {
$(document).ready(...)
. Современный эквивалент -$(function(){});