Я не могу понять, что случилось с моим скриптом и почему даже первое alert
не работает?
http://jsfiddle.net/TULsL/17/
HTML:
<div id="listProducts">
<input type="radio" class="selectItem" name="inch" id="inch-1" value="1" />
<label for="inch-1">Имя: тостер, Цвет: серый, Тип: мелкая техника, Цена: <span class="priceIn">500</span>, Количество единиц на складе: 5</label>
<br/>
<input type="radio" class="selectItem" name="inch" id="inch-2" value="4" />
<label for="inch-2">Имя: миксер, Цвет: черный, Тип: мелкая техника, Цена: <span class="priceIn">300</span>, Количество единиц на складе: 3</label>
<br/>
<input type="radio" class="selectItem" name="inch" id="inch-3" value="5" />
<label for="inch-3">Имя: электрочайник, Цвет: белый, Тип: мелкая техника, Цена: <span class="priceIn">320</span>, Количество единиц на складе: 2</label>
<br/>
</div>
<input type="text" id="priceValue" />
JavaScript:
$(document).ready(function () {
$('#listProducts input:radio:checked']).click(function () {
alert('test');
var text = $(this).next('label').find('.priceIn').html();
alert(text);
$('#priceValue').text(text);
});
});
удалите] и: checked и текст (текст) должен быть val (текст)
Оригинал:
$(document).ready(function () {
$('#listProducts input:radio:checked']).click(function () {
alert('test');
var text = $(this).next('label').find('.priceIn').html();
alert(text);
$('#priceValue').text(text);
});
});
Верный:
$(document).ready(function () {
$('#listProducts input:radio').click(function () {
alert('test');
var text = $(this).next('label').find('.priceIn').html();
alert(text);
$('#priceValue').val(text);
});
});
:checked
также и заменить $('#priceValue').text(text);
с $('#priceValue').val(text);
Вы совершаете ошибку, два - "]" и проверенное свойство.
$('#listProducts input:radio').click(function () {
alert('test');
var text = $(this).next('label').find('.priceIn').html();
alert(text);
$('#priceValue').val(text);
});
Вы забыли '
в конце".
$('#listProducts input:radio:checked]')
-------------------^--
также просто используйте $('#listProducts input:radio]')
вместо
$('#listProducts input:radio:checked]')
$(document).ready(function () {
// an extra ] at the end of the selector and you were registering the handler to only the checked radio button, in this case when the page loads there are no radio button which is checked
$('#listProducts input:radio').change(function () {
alert('test');
var text = $(this).next('label').find('.priceIn').html();
alert(text);
$('#priceValue').text(text);
});
});
Демо: скрипка
Также лучше использовать обработчик изменений вместо обработчика кликов