2009-10-06 69 views
4

我想動態檢查和取消選中與jquery框。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> 

我認爲最好的情況會設定一個變種,如果屬實,將設置複選框的值爲真。
事情我已經嘗試過包括:

$('.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。也沒有取消勾選鏈接時再次檢查這個我試圖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;,這將檢查所有複選框的另一個問題是沒有辦法取消選中它。

我仍然在這個項目的這個部分的學習,頭腦風暴部分,所以如果你有任何想法,他們會很棒。

+0

「我想我必須做這樣的事情」 - 你真的嘗試過嗎?你遇到任何問題? – 2009-10-06 02:36:07

+0

是的,我嘗試過了,而且玩了一段代碼。我遇到的問題是1)它會檢查類.edit_image_checkbox的所有框。 2)我可以通過使用$('。edit_image_checkbox')。attr('checked',true)來切換複選框。但不知道如何關閉它。我知道人們來到組織和團體時只是想要答案,這是多麼令人討厭。相信我,我在這裏學習,但在過去的6個小時裏堅持這一點! – BandonRandon 2009-10-06 02:58:57

+0

當你發佈html輸出而不是php循環時,你通常會得到更多的回覆。 – 2009-10-06 03:07:24

回答

4

我正確理解您的要求[什麼時候程序員不是:)],當用戶點擊編輯或刪除圖像時,您可以添加或刪除remove_project_image類到包含編輯/刪除鏈接的li。現在,如果類remove_project_image被添加到li您想要檢查複選框,否則清除它。下面的代碼片段可以做到這一點。

$('.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; 
}); 
+0

非常感謝你,如果我能,我會給你一個餅乾:)。這隻回答了一半我的問題也需要它,所以如果edit_project_file被點擊(意思是如果edit_project_image/$(this).parent()。next()被顯示)也檢查框。我修改了上面的函數並將其粘貼到.edit_project_file jquery中,使其能夠//知道編輯框是否顯示或者不是var r = $(this).parent()。next()。is(':visible' );'然後使用其餘的代碼。再次感謝! – BandonRandon 2009-10-06 16:37:45

+0

最後一件事情是如何取消勾選'.remove_edit_image'上的框? – BandonRandon 2009-10-06 16:45:43

+0

不要緊,拿着它,'var t = $(this).parent()。next();''''''''''''''' )一個。 – BandonRandon 2009-10-06 16:47:32

相關問題