php
  • javascript
  • checkbox
  • 2012-03-06 138 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 
    

    这只是为了检查/取消选中你的想法..发布的数据,你将只处理你现在正在做的方式,我想。

    相关问题