2011-09-23 85 views
1

請看看我的test page,我的問題是在「選擇圖片」組中查找所選單選按鈕的值。獲取組中選中的單選按鈕的值問題?

在裝載我用下面的代碼:

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

其左側的「選擇圖片」格拿起默認圖像。到現在爲止還挺好。

現在的問題是,當一個「選擇圖片」單選按鈕,隨後點擊我的事件代碼:

$("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> 

我缺少明顯的?

+0

如果使用change()事件而不是點擊操作,它會更好嗎? –

+0

你糾正了這個問題嗎?在Chrome,FF和IE中運行良好。 – rooney

+0

@SurrealDreams:它不應該,儘管change()可能會更可取。即使單選按鈕已被選中,點擊()將會觸發; change()不會。 – Blazemonger

回答

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> 
+0

使用$(document).ready(function(){}); – Thulasiram

+0

現場演示請參閱此鏈接:http://jsfiddle.net/nanoquantumtech/A5XYn/ – Thulasiram