获取组中选中的单选按钮的值时出现问题

Issue with obtaining the value of the checked radio button in a group?

本文关键字:问题 单选按钮 获取      更新时间:2023-09-26

请看看我的测试页面,我的问题是在"选择图片"组中找到所选单选按钮的值。

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

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>

我错过了显而易见的?? ?

<!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>