簡體   English   中英

Jquery選中並取消選中帶有鏈接的復選框

[英]Jquery check and uncheck checkbox with a link

我試圖用jquery動態檢查和取消選中一個框。

理想的情況是,如果單擊編輯項目文件,則會顯示上載新圖像的字段(edit_project_image)處於活動狀態,即不會通過再次單擊(edit_project_image)或單擊(remove_edit_image)刪除。 此外,如果有一個strikethough,我將添加類remove_project_image ,然后復選框也將被檢查。

這個想法是,如果上述任何一個都是真的,則需要刪除當前的活動圖像。

這是我現在的jquery:

//show hide the file box to edit images
$('.edit_project_file').live('click',function() {
    $(this).parent().next().toggle();
    $(this).parent().removeClass("remove_project_image");
    return false;
});

//allow the user to remove the file box if they don't wish to edit an image
$('.remove_edit_image').live('click',function() {
   $(this).parent().hide();
   $(this).parent().removeClass("remove_project_image");
    return false;
});

//add strikethough when the user decides to delete an image
$('.remove_project_file').live('click',function() {
   $(this).parent().toggleClass("remove_project_image");
   $(this).parent().next().hide();
   return false;
});

這是Html標記:

<ul class="imgPreview">
    <li>
        <a href="#" rel="../images/portfolio/project_images/manager_pimg3_7_1281126121.jpg">manager_pimg3_7_1281126121.jpg </a>
        <a href="#" class="edit_project_file"> <img src="images/edit.gif"/></a>  
        <a href="#" class="remove_project_file"> <img src="images/delete.gif"/></a>  
    </li>   
    <li class="edit_project_image">
        <input name="upload_project_images[]" type="file" />  
        <a href="#" class="remove_edit_image" border="0"> <img src="images/delete.gif" /></a>
    </li>  
    <li>
        <input name="edit_image[]" type="checkbox" value="manager_pimg3_7_1281126121.jpg" class="edit_image_checkbox"/>
    </li>  
</ul>

我相信最好的情況會設置一個var,如果為true,則將復選框的值設置為true。
我嘗試過的事情包括:

    $('.remove_project_file').live('click',function() {
   $(this).parent().toggleClass("remove_project_image");
   $(this).parent().next().hide();
   if ($(this).parent().next(".edit_image_checkbox").is(":not(:checked)")){
    $('.edit_image_checkbox').attr('checked',true);
    }

    return false;
});

這是因為它檢查了所有(上面是在php循環中)帶有.edit_image_checkbox類的復選框,而不僅僅是被單擊的remove_project_file類旁邊的remove_project_file 另外,當再次檢查鏈接時,沒有取消選中我嘗試了else {$('.edit_image_checkbox').attr('checked',true);}這只是混淆了它,如果它沒有被檢查然后檢查但是然后,如果檢查它取消選中它。

我的另一個想法是使用變量並將其設置為true或false,如果為true,則選中該框。 我嘗試過這樣的:

    var file_checkbox_checked = false;
if(file_checkbox_checked ==  true){
$('.edit_image_checkbox').attr('checked',true);
}
else{
$('.edit_image_checkbox').attr('checked',false);
}

然后添加了file_checkbox_checked = true; 應該選中復選框的每個函數。 這似乎什么都不做。 我可以設置var file_checkbox_checked = true; 這將檢查所有復選框的另一個問題是,沒有辦法取消選中它。

我仍然在學習,集思廣益這個項目的一部分,所以如果你有任何想法他們會很棒。

我理解你的要求[程序員什么時候沒有:)],當用戶點擊編輯或刪除你添加的圖像或刪除remove_project_image類到包含編輯/刪除鏈接的li 現在,如果將類remove_project_image添加到li remove_project_image檢查復選框,否則清除它。 以下代碼片段可以做到這一點。

$('.remove_project_file').live('click',function() {
    $(this).parent().toggleClass("remove_project_image");
    //TO KNOW IF STRIKE-THROUGH IS APPLIED TO IT OR NOT
    var r = $(this).parent().hasClass("remove_project_image");
    $(this).parent().next().hide();

    //WE NEED TO GET THE LI CONTAINING CHECK BOX, ONE NEXT TO HIDDEN ONE
    var t = $(this).parent().next().next();
    //GET CHECKBOX
    var c=$(".edit_image_checkbox", t);
    $(c).attr('checked',r);
    return false;
});

暫無
暫無

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

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