我想動態檢查和取消選中與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;
,這將檢查所有複選框的另一個問題是沒有辦法取消選中它。
我仍然在這個項目的這個部分的學習,頭腦風暴部分,所以如果你有任何想法,他們會很棒。
「我想我必須做這樣的事情」 - 你真的嘗試過嗎?你遇到任何問題? – 2009-10-06 02:36:07
是的,我嘗試過了,而且玩了一段代碼。我遇到的問題是1)它會檢查類.edit_image_checkbox的所有框。 2)我可以通過使用$('。edit_image_checkbox')。attr('checked',true)來切換複選框。但不知道如何關閉它。我知道人們來到組織和團體時只是想要答案,這是多麼令人討厭。相信我,我在這裏學習,但在過去的6個小時裏堅持這一點! – BandonRandon 2009-10-06 02:58:57
當你發佈html輸出而不是php循環時,你通常會得到更多的回覆。 – 2009-10-06 03:07:24