簡體   English   中英

單選按鈕到動態創建的圖像中

[英]Radio button into images created dynamically

我想通過單擊動態創建的圖像來更改選中的收音機 btn。 這是生成我的圖像的 PHP:

<?php if($subject_list){
      foreach ($subject_list as $item):?>
            <input type="radio" name="subjectagendaactivity" value="male" />
            <img src="/static/images/school/students/subjects/island/island_<?php echo $item['id'];?>_agenda.png"/>
      <?php endforeach;}?>

所以我的圖像是這樣生成的,這是我在 JQuery 中制作的代碼,以便能夠更改.cheked:

$(this).click(function () {
                $('input:radio[name=subjectagendaactivity]')[0].checked = true;
            });

我需要將 [0] 更改為 $(this) 或類似的東西,以便在我單擊正確的圖像時檢查我的收音機 btn。

像這樣的東西對你有用嗎?: http://jsfiddle.net/FsshJ/唯一的缺點是它需要imginput都有 ID。 但是,如果您願意/需要,您可以將其更改為其他內容。

嘗試這個:

$(this).click(function () {
                $('input:radio[name=subjectagendaactivity]').attr("checked", true);
            });
$(this).click(checkIt($(this)));



// Elsewhere
function checkIt(rBtn) {
    rBtn.checked = true;
}

嘗試這個:

$('input:radio[name=subjectagendaactivity]').each(function(index) {
if(index == 0)
{
     $(this).attr("checked","checked");
     return;
}
});

假設您要檢查的復選框始終是您單擊的圖像之前的元素:

$('img').click(function () {
    var $check = $(this).prev(':checkbox');

    if($check.is(':checked'){  // Already checked? Uncheck it!
        $check.removeAttr('checked');
    }
    else{
        $check.attr('checked','checked');
    }
});

看看jQuery的遍歷方法 您應該能夠找到一個可以讓您按慣例獲取復選框的選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM