2015-09-04 32 views
0

我到處搜索過,我似乎無法得到任何工作。我想在通過按鈕刪除所有行後沒有顯示任何錶行時隱藏「刪除所選項目」按鈕。我嘗試了幾個JQuery函數無濟於事。如何根據表是否有行來動態地隱藏按鈕

我目前有onclick事件調用javascript函數來設置按鈕的可見性隱藏或可見,但似乎並沒有工作。我已經能夠通過添加按鈕使按鈕可見,但我似乎無法在表格爲空後再次隱藏它。提前致謝。

<!DOCTYPE html> 
<html> 
<head> 
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> 
<link rel="stylesheet" type="text/css" href="../stylesheet.css"> 

<script type="text/javascript"> 

function checkFN(){ //function to check if there are rows in the table 
    var x = document.getElementById("tasksFN").rows.length; //gives a variable to row length 
    if(x == 0){ //checks if there is any data in table 
     //if no data in table hides delete button 
     document.getElementById("delFN").style.visibility = "hidden"; 
    }else{ 
     //if data exists in table makes delete button visible 
     document.getElementById("delFN").style.visibility = "visible"; 
    } 
} 
</script> 
<script type="text/javascript"> 
function checkWM(){ //function to check if there are rows in the table 
    var x = document.getElementById("tasksWM").rows.length; //gives a variable to row length 
    if(x == 0){ //checks if there is any data in table 
     //if no data in table hides delete button 
     document.getElementById("delWM").style.visibility = "hidden"; 
    }else{ 
     //if data exists in table makes delete button visible 
     document.getElementById("delWM").style.visibility = "visible"; 
    } 
} 
</script> 
<script type="text/javascript"> 
function checkBOH(){ //function to check if there are rows in the table 
    var x = document.getElementById("tasksBOH").rows.length; //gives a variable to row length 
    if(x == 0){ //checks if there is any data in table 
     //if no data in table hides delete button 
     document.getElementById("delBOH").style.visibility = "hidden"; 
    }else{ 
     //if data exists in table makes delete button visible 
     document.getElementById("delBOH").style.visibility = "visible"; 
    } 
} 
</script> 
<script type="text/javascript"> 
function checkRAM(){ //function to check if there are rows in the table 
    var x = document.getElementById("tasksRAM").rows.length; //gives a variable to row length 
    if(x == 0){ //checks if there is any data in table 
     //if no data in table hides delete button 
     document.getElementById("delRAM").style.visibility = "hidden"; 
    }else{ 
     //if data exists in table makes delete button visible 
     document.getElementById("delRAM").style.visibility = "visible"; 
    } 
} 
</script> 

</head> 

<body> 
<H2 align="center"><u>Enter New Tasks Below</u></H2> 
<H3 align="center"> 
Type your new task into the text box below and click "Add".<br><br> 
Once a task or multiple tasks have been completed, check the box next to the task/tasks you would like to remove and click the "Delete Selected Items" button.</H3> 

<div id="main" align"center"> 
<div class="unconFN"> 
    <table id="tasksFN" cellpadding="3" cellspacing="3" border="0"> 
     <h4>FieldNation UnConfirmed WO Numbers</h4> <!-- textbox heading --> 
     <input type="text" id="newFN" /> <!-- textbox for new data to be added --> 
     <br /> 
     <input type="submit" id="addFN" value="Add" /> <!--button to add data from textbox to table --> 
     <br /><br /> 
    </table/> 
<br /> 
    <input type="button" id="delFN" value="Delete Selected Items" hidden="true" onclick="checkFN()" /> 

    <script> 
     //function to add text from textbox to table on button click 
     $("#addFN").click(function() { 
      //data to be added to table 
      var val = $("#newFN").val(); 
      //prepends checkbox to data added to table 
      var newTxt = $('<tr><td><input type="checkbox" class="checkbox"></td><td>' + val + '</td></tr>'); 

    //creates new tr and td from the values entered in textbox 
    $('#myTable').append('<tr><td>' + val + '</td></tr>'); 
      //adds the data to the table 
      $("#tasksFN").append(newTxt); 
      //creates new empty row for next data set to be added 
      $("#newFN").val(""); 
      //changes hidden attribute of delete button to make it visible 
      $("#delFN").attr("hidden",false); 
     }); 

     $(document).ready(function() { 

      //function to delete checked rows from table on button click 
      $("#delFN").click(function() { 
       //checks to see whether checkbox is checked or not 
       console.log($(".checkbox[checked='checked']")) 
       //function to do an action on all rows with checked checkboxes 
       $(".checkbox:checked").each(function() { 
        //sets variable for the rows with checked checkboxes 
        var curFN = $(this).parents('tr'); 
        //deletes rows with checked checkboxes 
        curFN.remove(); 
       }); 
      }); 
     }); 
    </script> 
    </div> <!-- end .unconFN div --> 

<div class="unconWM"> 
    <table id="tasksWM" cellpadding="3" cellspacing="3" border="0"> 
     <h4>WorkMarket UnConfirmed WO Numbers</h4> <!-- textbox heading --> 
     <input type="text" id="newWM" /> <!-- textbox for new data to be added --> 
     <br /> 
     <input type="submit" id="addWM" value="Add" /> <!--button to add data from textbox to table --> 
     <br /><br /> 
    </table/> 
<br /> 
    <input type="button" id="delWM" value="Delete Selected Items" hidden="true" onclick="checkWM()" /> 

    <script> 
     //function to add text from textbox to table on button click 
     $("#addWM").click(function() { 
      //data to be added to table 
      var val = $("#newWM").val(); 
      //prepends checkbox to data added to table 
      var newWM = $('<tr><td><input type="checkbox" class="checkbox"></td><td>' + val + '</td></tr>'); 

    //creates new tr and td from the values entered in textbox 
    $('#myTable').append('<tr><td>' + val + '</td></tr>'); 
      //adds the data to the table 
      $("#tasksWM").append(newWM); 
      //creates new empty row for next data set to be added 
      $("#newWM").val(""); 
      //changes hidden attribute of delete button to make it visible 
      $("#delWM").attr("hidden",false); 
     }); 

     $(document).ready(function() { 

      //function to delete checked rows from table on button click 
      $("#delWM").click(function() { 
       //checks to see whether checkbox is checked or not 
       console.log($(".checkbox[checked='checked']")) 
       //function to do an action on all rows with checked checkboxes 
       $(".checkbox:checked").each(function() { 
        //sets variable for the rows with checked checkboxes 
        var curWM = $(this).parents('tr'); 
        //deletes rows with checked checkboxes 
        curWM.remove(); 
       }); 
      }); 
     }); 
    </script> 
    </div> <!-- end .unconWM div --> 

<div class="BOHswap"> 
    <table id="tasksBOH" cellpadding="3" cellspacing="3" border="0"> 
     <h4>FieldNation UnConfirmed WO Numbers</h4> <!-- textbox heading --> 
     <input type="text" id="newBOH" /> <!-- textbox for new data to be added --> 
     <br /> 
     <input type="submit" id="addBOH" value="Add" /> <!--button to add data from textbox to table --> 
     <br /><br /> 
    </table/> 
<br /> 
    <input type="button" id="delBOH" value="Delete Selected Items" hidden="true" onclick="checkBOH()" /> 

    <script> 
     //function to add text from textbox to table on button click 
     $("#addBOH").click(function() { 
      //data to be added to table 
      var val = $("#newBOH").val(); 
      //prepends checkbox to data added to table 
      var newTxt = $('<tr><td><input type="checkbox" class="checkbox"></td><td>' + val + '</td></tr>'); 

    //creates new tr and td from the values entered in textbox 
    $('#myTable').append('<tr><td>' + val + '</td></tr>'); 
      //adds the data to the table 
      $("#tasksBOH").append(newTxt); 
      //creates new empty row for next data set to be added 
      $("#newBOH").val(""); 
      //changes hidden attribute of delete button to make it visible 
      $("#delBOH").attr("hidden",false); 
     }); 

     $(document).ready(function() { 

      //function to delete checked rows from table on button click 
      $("#delBOH").click(function() { 
       //checks to see whether checkbox is checked or not 
       console.log($(".checkbox[checked='checked']")) 
       //function to do an action on all rows with checked checkboxes 
       $(".checkbox:checked").each(function() { 
        //sets variable for the rows with checked checkboxes 
        var curBOH = $(this).parents('tr'); 
        //deletes rows with checked checkboxes 
        curBOH.remove(); 
       }); 
      }); 
     }); 
    </script> 
    </div> <!-- end .BOHswap div --> 

<div class="unRAM"> 
    <table id="tasksRAM" cellpadding="3" cellspacing="3" border="0"> 
     <h4>FieldNation UnConfirmed WO Numbers</h4> <!-- textbox heading --> 
     <input type="text" id="newRAM" /> <!-- textbox for new data to be added --> 
     <br /> 
     <input type="submit" id="addRAM" value="Add" /> <!--button to add data from textbox to table --> 
     <br /><br /> 
    </table/> 
<br /> 
    <input type="button" id="delRAM" value="Delete Selected Items" hidden="true" onclick="checkRAM()" /> 

    <script> 

     //function to add text from textbox to table on button click 
     $("#addRAM").click(function() { 
      //data to be added to table 
      var val = $("#newRAM").val(); 
      //prepends checkbox to data added to table 
      var newTxt = $('<tr><td><input type="checkbox" class="checkbox"></td><td>' + val + '</td></tr>'); 

     //creates new tr and td from the values entered in textbox 
     $('#myTable').append('<tr><td>' + val + '</td></tr>'); 
      //adds the data to the table 
      $("#tasksRAM").append(newTxt); 
      //creates new empty row for next data set to be added 
      $("#newRAM").val(""); 
      //changes hidden attribute of delete button to make it visible 
      $("#delRAM").attr("hidden",false); 
     }); 

     $(document).ready(function() { 

      //function to delete checked rows from table on button click 
      $("#delRAM").click(function() { 
       //checks to see whether checkbox is checked or not 
       console.log($(".checkbox[checked='checked']")) 
       //function to do an action on all rows with checked checkboxes 
       $(".checkbox:checked").each(function() { 
        //sets variable for the rows with checked checkboxes 
        var curRAM = $(this).parents('tr'); 
        //deletes rows with checked checkboxes 
        curRAM.remove(); 
       }); 
      }); 
     }); 
    </script> 
    </div> <!-- end .unRAM div --> 
</div> <!-- end #main div --> 
</body> 
</html> 
+0

使用'document.getElementById'不是'Document.getElementById' –

+0

好抓,菜鳥錯誤,還是不行。 :-(但是我已經更新了代碼以反映這個變化 – user3321457

+0

在你提供的代碼中,所有函數都沒有關閉'}' – RRK

回答

1

首先幾點建議。你有幾個語法問題,而且代碼沒有太多可讀性。始終不要將腳本與html混淆。閱讀真的很痛苦。以下是更新的htmljs和工作Fiddle here

$(document).ready(function() { 
 
//keep only one document.ready if possible and wrap all the js code in that 
 
function checkFN(){ 
 
    var x = document.getElementById("tasksFN").rows.length; 
 
    if(x == 0){ 
 
     $("#delFN").hide(); 
 
     //since you are anyways using jquery make use of it completely. Use .hide() and 
 
     //.show() jquery methods to hide/show elements 
 
    }else{ 
 
     $("#delFN").show(); 
 
    } 
 
} 
 

 
function checkWM(){ 
 
    var x = document.getElementById("tasksWM").rows.length; 
 
    if(x == 0){ 
 
     $("#delWM").hide(); 
 
    }else{ 
 
     $("#delWM").show(); 
 
    } 
 
} 
 

 
function checkBOH(){ 
 
    var x = document.getElementById("tasksBOH").rows.length; 
 
    if(x == 0){ 
 
     $("#delBOH").hide(); 
 
    }else{ 
 
     $("#delBOH").show(); 
 
    } 
 
} 
 

 
function checkRAM(){ 
 
    var x = document.getElementById("tasksRAM").rows.length; 
 
    if(x == 0){ 
 
     document.getElementById("delRAM").style.visibility = "hidden"; 
 
    }else{ 
 
     document.getElementById("delRAM").style.visibility = "visible"; 
 
    } 
 
} 
 

 
$("#delWM").click(function() { 
 
    $(".checkbox:checked").each(function() { 
 
     $(this).closest('tr').remove(); 
 
    }); 
 
    checkWM(); 
 
}); 
 

 
$("#addWM").click(function() { 
 
    var val = $("#newWM").val(); 
 
    var newWM = $('<tr><td><input type="checkbox" class="checkbox"></td><td>' + val + '</td></tr>'); 
 
\t $('#myTable').append('<tr><td>' + val + '</td></tr>'); 
 
    $("#tasksWM").append(newWM); 
 
    $("#newWM").val(""); 
 
    $("#delWM").show(); 
 
}); 
 
     
 
$("#addFN").click(function() { 
 
    var val = $("#newFN").val(); 
 
    var newTxt = $('<tr><td><input type="checkbox" class="checkbox"></td><td>' + val + '</td></tr>'); 
 
    $('#myTable').append('<tr><td>' + val + '</td></tr>'); 
 
    $("#tasksFN").append(newTxt); 
 
    $("#newFN").val(""); 
 
    $("#delFN").show(); 
 
}); 
 

 
$("#delFN").click(function() { 
 
    $(".checkbox:checked").each(function() { 
 
     $(this).closest('tr').remove(); 
 
    }); 
 
    checkFN();//call the required function to check for data to work with delete button hide/show 
 
}); 
 
    
 
$("#addBOH").click(function() { 
 
    var val = $("#newBOH").val(); 
 
    var newTxt = $('<tr><td><input type="checkbox" class="checkbox"></td><td>' + val + '</td></tr>'); 
 
    $('#myTable').append('<tr><td>' + val + '</td></tr>'); 
 
    $("#tasksBOH").append(newTxt); 
 
    $("#newBOH").val(""); 
 
    $("#delBOH").show(); 
 
}); 
 
    
 
$("#delBOH").click(function() { 
 
    $(".checkbox:checked").each(function() { 
 
     $(this).closest('tr').remove(); 
 
    }); 
 
    checkBOH(); 
 
}); 
 

 
$("#addRAM").click(function() { 
 
    var val = $("#newRAM").val(); 
 
    var newTxt = $('<tr><td><input type="checkbox" class="checkbox"></td><td>' + val + '</td></tr>'); 
 
    $('#myTable').append('<tr><td>' + val + '</td></tr>'); 
 
    $("#tasksRAM").append(newTxt); 
 
    $("#newRAM").val(""); 
 
    $("#delRAM").show(); 
 
}); 
 
    
 
$("#delRAM").click(function() { 
 
\t $(".checkbox:checked").each(function() { 
 
      $(this).closest('tr').remove(); 
 
    }); 
 
    checkRAM(); 
 
}); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<h2 align="center"><u>Enter New Tasks Below</u></h2> 
 
<h3 align="center"> 
 
Type your new task into the text box below and click "Add".<br/><br/> 
 
Once a task or multiple tasks have been completed, check the box next to the task/tasks you would like to remove and click the "Delete Selected Items" button.</h3> 
 
<div class="unconFN"> 
 
    <table id="tasksFN" cellpadding="3" cellspacing="3" border="0"> 
 
     <h4>FieldNation UnConfirmed WO Numbers</h4> 
 
     <input type="text" id="newFN" /> 
 
     <br /> 
 
     <input type="submit" id="addFN" value="Add" /> 
 
     <br /><br /> 
 
    </table> 
 
    <br /> 
 
    <input type="button" id="delFN" value="Delete Selected Items" hidden="true" /> 
 
</div> 
 
<div class="unconWM"> 
 
    <table id="tasksWM" cellpadding="3" cellspacing="3" border="0"> 
 
     <h4>WorkMarket UnConfirmed WO Numbers</h4> 
 
     <input type="text" id="newWM" /> 
 
     <br /> 
 
     <input type="submit" id="addWM" value="Add" /> 
 
     <br /><br /> 
 
    </table> 
 
    <br /> 
 
    <input type="button" id="delWM" value="Delete Selected Items" hidden="true" /> 
 
</div> 
 

 
<div class="BOHswap"> 
 
    <table id="tasksBOH" cellpadding="3" cellspacing="3" border="0"> 
 
     <h4>FieldNation UnConfirmed WO Numbers</h4> 
 
     <input type="text" id="newBOH" /> 
 
     <br /> 
 
     <input type="submit" id="addBOH" value="Add" /> 
 
     <br /><br /> 
 
    </table> 
 
    <br /> 
 
    <input type="button" id="delBOH" value="Delete Selected Items" hidden="true" /> 
 
</div> 
 

 
<div class="unRAM"> 
 
    <table id="tasksRAM" cellpadding="3" cellspacing="3" border="0"> 
 
     <h4>FieldNation UnConfirmed WO Numbers</h4> 
 
     <input type="text" id="newRAM" /> 
 
     <br /> 
 
     <input type="submit" id="addRAM" value="Add" /> 
 
     <br /><br /> 
 
    </table> 
 
    <br /> 
 
    <input type="button" id="delRAM" value="Delete Selected Items" hidden="true" /> 
 
</div>

幾個關鍵點需要注意

  • <table>沒有結束正常。這就像</table/>
    而它應該是</table>

  • 不要像<input type="button" id="delRAM" onclick="somefunc()"/>元素內嵌寫onclick,因爲你已經爲按鈕操作 click event。會發生什麼情況是該功能將首先調用 ,它會檢查記錄是否存在,然後它會去 並刪除每個checkbox。所以你需要在 $.each之後調用function來檢查記錄。

  • 總是儘量保持一個$(document).ready(),敷在你的所有js
    代碼,這$(document).ready()是好的,如果你保持</body>js只是
    之前部分應該總是在html和 結束不要忘記保持$(document).ready()的內部<script> type="text/javascipt"></script>

+1

我真的很感謝像你這樣的人在深入分析和建議方面的幫助真的讓我們所有人都變得更好,你們已經提出了一些很好的觀點,並且我會將你的建議放在心中以備我的未來項目,並且非常感謝你們的幫助! – user3321457

+0

隨時隨地..快樂編碼.. :) –

0
  1. 添加http://腳本標記的src

  2. 添加右括號,首先4個功能

這些變化的代碼似乎正常工作之後。

0

出於興趣,這裏是我的嘗試:

var myTable = $('#myTable'); 
 
var prefixAddID = 'add'; 
 
var prefixDelID = 'del'; 
 
var prefixTasksID = 'tasks'; 
 
var prefixNewID = 'new'; 
 

 
$('#delFN, #delWM, #delBOH, #delRAM').on('click', deleteRow); 
 
$('#addFN, #addWM, #addBOH, #addRAM').on('click', addRow); 
 

 
function addRow() { 
 
    var suffixID = $(this).attr('id').substr(prefixAddID.length); 
 
    var value = $('#' + prefixNewID + suffixID).val(); 
 
    myTable.append('<tr><td>' + value + '</td></tr>'); 
 
    $('#' + prefixTasksID + suffixID).append($('<tr><td><input type="checkbox" class="checkbox"></td><td>' + value + '</td></tr>')); 
 
    $('#' + prefixNewID + suffixID).val(''); 
 
    $('#' + prefixDelID + suffixID).show(); 
 
} 
 

 
function deleteRow() { 
 
    var suffixID = $(this).attr('id').substr(prefixDelID.length); 
 
    $('.checkbox:checked').each(function() { 
 
    $(this).parents('tr').remove(); 
 
    }); 
 
    var length = $('#' + prefixTasksID + suffixID)[0].rows.length; 
 
    $('#' + prefixDelID + suffixID).toggle(length !== 0); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<h2 align="center"><u>Enter New Tasks Below</u></h2> 
 
<h3 align="center"> 
 
Type your new task into the text box below and click "Add".<br><br> 
 
Once a task or multiple tasks have been completed, check the box next to the task/tasks you would like to remove and click the "Delete Selected Items" button.</h3> 
 
<div class="unconFN"> 
 
    <table id="tasksFN" cellpadding="3" cellspacing="3" border="0"> 
 
    <h4>FieldNation UnConfirmed WO Numbers</h4> 
 
    <input type="text" id="newFN" /> 
 
    <br /> 
 
    <input type="submit" id="addFN" value="Add" /> 
 
    <br /> 
 
    <br /> 
 
    </table> 
 
    <br /> 
 
    <input type="button" id="delFN" value="Delete Selected Items" hidden="true" /> 
 
</div> 
 
<div class="unconWM"> 
 
    <table id="tasksWM" cellpadding="3" cellspacing="3" border="0"> 
 
    <h4>WorkMarket UnConfirmed WO Numbers</h4> 
 
    <input type="text" id="newWM" /> 
 
    <br /> 
 
    <input type="submit" id="addWM" value="Add" /> 
 
    <br /> 
 
    <br /> 
 
    </table> 
 
    <br /> 
 
    <input type="button" id="delWM" value="Delete Selected Items" hidden="true" /> 
 
</div> 
 
<div class="BOHswap"> 
 
    <table id="tasksBOH" cellpadding="3" cellspacing="3" border="0"> 
 
    <h4>FieldNation UnConfirmed WO Numbers</h4> 
 
    <input type="text" id="newBOH" /> 
 
    <br /> 
 
    <input type="submit" id="addBOH" value="Add" /> 
 
    <br /> 
 
    <br /> 
 
    </table> 
 
    <br /> 
 
    <input type="button" id="delBOH" value="Delete Selected Items" hidden="true" /> 
 
</div> 
 
<div class="unRAM"> 
 
    <table id="tasksRAM" cellpadding="3" cellspacing="3" border="0"> 
 
    <h4>FieldNation UnConfirmed WO Numbers</h4> 
 
    <input type="text" id="newRAM" /> 
 
    <br /> 
 
    <input type="submit" id="addRAM" value="Add" /> 
 
    <br /> 
 
    <br /> 
 
    </table> 
 
    <br /> 
 
    <input type="button" id="delRAM" value="Delete Selected Items" hidden="true" /> 
 
</div>

的jsfiddle其中可以發現here。希望這有助於某種方式。我會在稍後添加細節。

詳細

  • 的想法是,你應該只有一個addRow功能和一個deleteRow功能,以避免重複。
  • 由於您已經在您的HTML元素中爲您的id s使用了命名約定,因此這成爲可能。他們都有類似的模式。
  • 我不會說在你的標記中最好使用如此多的id,因爲它們可以用class屬性替代,但現在,我只修正了標記中的一些問題(如其他人所述) ,使用了幾乎相同的標記。
  • 因此,在addRow中,點擊的Add按鈕的唯一性由其idsuffix等取得。 FNWM
  • 其餘的操作只是使用這個獨特的後綴做任何需要做的事情。
  • 例如,當點擊addFN按鈕時,tasksFN需要在其中添加某個標記。因此,我們從addFN中取出FN,我們將其與tasks結合以製作tasksFN選擇器。
  • 其餘的是不言自明的。
  • 類似的東西發生在deleteRow函數。