获取组中已检查单选按钮的值的问题?

时间:2011-09-23 15:38:28

标签: javascript jquery

请查看我的test page,我的问题是在“选择图片”组中查找所选单选按钮的值。

加载时我使用了以下代码:

var currentImageFile = $("input:radio[name=selectedImage]:checked").val();

选择左侧“Selected picture”div的默认图像。到目前为止一切都很好。

现在问题是随后点击了“选择图片”单选按钮我的事件代码:

$("input:radio[name=selectedImage]").click(function() {     
  currentImageFile = $(this).val();
  alert($(this).val());  
});

(警报仅供测试)根本不返回任何值!

HTML很简单:

<li><div class="xmasImage rounded" rel="http://www.completeinsight.co/resources/xmasHR/56269.jpg">
    <img class="rounded" src="http://www.completeinsight.co/resources/xmasLR/56269_p.jpg" width="65" height="65" border="0" alt="56269" />
    <div class="imageSelect"><input type="radio" name="selectedImage" value="56269">
        &nbsp;<label for="selectedImage">56269</label>
    </div>
</div></li>

我错过了明显的??

1 个答案:

答案 0 :(得分:0)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var currentImageFile = '';
            $('#ImageSelection').find("input:radio[name=selectedImage]").click(function () {
                currentImageFile = $(this).val();
                alert($(this).val());
            });
        });
    </script>
</head>
<body>
    <ul id="ImageSelection">
        <li>
            <div class="xmasImage rounded" rel="http://www.completeinsight.co/resources/xmasHR/56269.jpg">
                <img class="rounded" src="http://www.completeinsight.co/resources/xmasLR/56269_p.jpg"
                    width="65" height="65" border="0" alt="56269" />
                <div class="imageSelect">
                    <input type="radio" name="selectedImage" value="56269">
                    &nbsp;<label for="selectedImage">56269</label>
                </div>
            </div>
        </li>
        <li>
            <div class="xmasImage rounded" rel="http://www.completeinsight.co/resources/xmasHR/56270.jpg">
                <img class="rounded" src="http://www.completeinsight.co/resources/xmasLR/56270_p.jpg"
                    width="65" height="65" border="0" alt="56270" />
                <div class="imageSelect">
                    <input type="radio" name="selectedImage" value="56270">
                    &nbsp;<label for="selectedImage">56270</label>
                </div>
            </div>
        </li>
    </ul>
</body>
</html>