“未知的伪类或伪元素'无线电'”

时间:2009-11-24 07:33:39

标签: jquery jquery-selectors radio-button pseudo-element

网页上有许多输入无线电元素,每个无线电元素有几个选项。我想要在检查无线电元素的一个选项时触发函数。如何使用Jquery编写代码?一个输入元素如下所示:

<div class="below">
<input type="radio" value="information" name="option0"/>
information
<input type="radio" value="communication" name="option0"/>
communication
<input type="radio" value="goods" name="option0"/>
goods
<input type="radio" value="attention" name="option0"/>
attention
</div>

我编写的代码如下:

$('input:radio').click(function () {
  if (this.checked) { // or $(this).attr('checked')
    alert(this.value); // or alert($(this).val());
  }
});

我的Jquery版本是

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

我正在使用Firefox。在错误控制台上,它显示“未知的伪类或伪元素'无线电'”。任何的想法?怎么了?

我之前的问题接近这个问题,但到目前为止我还没有得到解决方案。所以我不得不四处询问。希望这个问题不会被视为一个重复的问题。谢谢!

3 个答案:

答案 0 :(得分:1)

也许这个?

$('input[type=radio]').click( ... );

答案 1 :(得分:0)

输入元素通常包含在表单元素中,是你的情况吗?如果是这样,您可能需要在选择器中包含表单元素。请在此处查看示例:http://docs.jquery.com/Selectors/radio

答案 2 :(得分:0)

我已经使用您的代码设置了一个简单的测试页面,尽管我在控制台中遇到相同的错误,但它仍然有效(每当我点击单选按钮时都会显示警告消息)。我在这里发布页面代码,以防它找到你错过的东西:

<html>
<header>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('input:radio').click(function () {
  if (this.checked) { // or $(this).attr('checked')
    alert(this.value); // or alert($(this).val());
  }
});
});
</script>
</header>
<body>
<div class="below">
<input type="radio" value="information" name="option0"/>
information
<input type="radio" value="communication" name="option0"/>
communication
<input type="radio" value="goods" name="option0"/>
goods
<input type="radio" value="attention" name="option0"/>
attention
</div>
</body>
</html>
相关问题