2013-12-14 152 views
0

我不能理解我的脚本有什么问题,为什么即使第一个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); 
    }); 
}); 
+0

为http://的jsfiddle .net/TULsL/25/ –

回答

0

删除]和:检查和文本(文本)应该是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); 
    }); 
}); 
+0

它没有帮助( –

+1

我发现,我需要删除':checked'并且用'$('#priceValue')。val(text)'替换'$('#priceValue').text(text);'' –

+0

很高兴你找到它了,我把它添加到我的回答:) – fonZ

0

你犯错两个一为 ']',并检查属性..

$('#listProducts input:radio').click(function() { 
      alert('test'); 
      var text = $(this).next('label').find('.priceIn').html(); 
      alert(text); 
      $('#priceValue').val(text); 
     }); 
0

你在最后忘记'

$('#listProducts input:radio:checked]') 
        -------------------^-- 

也只是使用的$('#listProducts input:radio]')代替

$('#listProducts input:radio:checked]') 
+0

但是然后你打开报价两次,关闭一次,或打开一次,关闭两次,应该是? – fonZ

+0

@fonZ ohh很好的接收...我忘了。 –

0
$(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); 
    }); 
}); 

演示:Fiddle

而且最好使用更改处理代替单击处理