php
  • javascript
  • checkbox
  • 2012-03-06 114 views 0 likes 
    0

    我有一個複選框,將其狀態(選中/取消選中)保存在MySQL數據庫中。例如有一個在https://encrypted.coxnetworks.co.uk/school/includes/Checkboxes/Monday1/checkbox.php(它受密碼保護)。取消選中複選框(在數據庫中保存狀態)在PHP中

    頁面上的代碼被

    <form id='checkbox' method="post" action='/school/includes/Checkboxes/Monday1/checkbox.php'><!-- php: get the name+location of the webpage --> 
        <input type="hidden" name="confirm" value="1"> <!-- give confirm a value --> 
        <input type="checkbox" checked name="checkbox" value="1" onclick="document.forms['checkbox'].submit()"/> <!-- submit the form when checkbox is clicked --> 
    </form> 
    

    因此它使用JavaScript張貼到PHP。不過,我希望能夠點擊一個按鈕啓動腳本來取消選中這些腳本的負載。我怎麼能這樣做?

    +0

    雖然它們是通過iFrames「包含」的,但不是直接在頁面中,所以javascript解決方案並不真正起作用。對不起應該說了。 – 2012-03-06 18:07:22

    回答

    0

    你不需要多種形式,你可以用jQuery的AJAX做到這一點:

    $(function() { 
        $('input.save_state').change(function)() { 
         $.ajax({ 
         url: '/path/to/controller', 
         type: 'POST', 
         // Send both the ID and the boolean value 
         data: 'id='+$(this).attr('id')+'&val='+$(this).val(), 
         success: function() { 
          // show success message if necessary 
         } 
         }); 
        }); 
        $('button.wipe_all_states').change(function)() { 
         $.ajax({ 
         url: '/path/to/controller', 
         type: 'POST', 
         // Because you're resetting everything, you don't need to send IDs or values 
         success: function() { 
          // show success message if necessary 
         } 
         }); 
        }); 
    }); 
    
    0

    我會建議使用jQuery。這將允許你使用一個簡單的腳本,如:

    $('input[type=checkbox].uncheck').attr('checked',false); 
    

    這將取消選中與「取消」類的所有複選框。

    您可以找到jQuery的:

    http://jquery.com/

    0

    一些東西到單獨

    的JavaScript擴展:

    function checkAll(field) 
    { 
    for (i = 0; i < field.length; i++) 
        field[i].checked = true ; 
    } 
    
    function uncheckAll(field) 
    { 
    for (i = 0; i < field.length; i++) 
        field[i].checked = false ; 
    } 
    

    或一個jQuery再現可能看起來像:

    $(".fieldClass").attr("checked", "checked"); // make checkbox or radio checked 
    $(".fieldClass").removeAttr("checked"); // uncheck the checkbox or radio 
    

    這只是爲了檢查/取消選中你的想法..發佈的數據,你將只處理你現在正在做的方式,我想。

    相關問題