php php函数中的复选框onclick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14907757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:10:50  来源:igfitidea点击:

checkbox onclick in php function

phpcheckbox

提问by MGR

Can i write some code to execute while my check box is checked in my php code..

当我的复选框在我的 php 代码中被选中时,我可以编写一些代码来执行吗?

my declaration of check box is...

我的复选框声明是...

<input id="checkbox" name="click" type="checkbox" onclick="check(this)"/>

i thought to perform a function called check()while clicking the check box..

我想check()在单击复选框时执行一个调用的函数..

 <script type="text/javascript">
  function check(cb)
  {
      if($("input[type=checkbox]:checked"")
      {
        //my functionality and operations
      }
  }

But its not working, how can i perform the onclick event in the Checkbox's action..

但它不起作用,我如何在复选框的操作中执行 onclick 事件..

回答by Deadlock

$('#myform :checkbox').click(function() {
    var $this = $(this);
    // $this will contain a reference to the checkbox   
    if ($this.is(':checked')) {
        // the checkbox was checked 
    } else {
        // the checkbox was unchecked
    }
});

Where your form has id myform

您的表单有 id 的地方 myform

回答by Praveen Kumar Purushothaman

First of all, there's a mistake. It should be .is(":checked").

首先,有一个错误。应该是.is(":checked")

function check(cb)
{
    if($(cb).is(":checked"))
    {
        //my functionality and operations
    }
}

And the HTML should be:

并且 HTML 应该是:

<input type="checkbox" onclick="check(this);" />


Or, if you wanna invoke a PHP Function after clicking on Checkbox, you need to write an AJAX code. If this is the case, in your ifcondition, and checked condition, you can call a PHP file, that calls only this function.

或者,如果您想在单击 Checkbox 后调用 PHP 函数,则需要编写 AJAX 代码。如果是这种情况,在您的if条件和检查条件下,您可以调用仅调用此函数的 PHP 文件。

function check(cb)
{
    if($(cb).is(":checked"))
    {
        $.getScript("clickCheckbox.php");
    }
}

And you can write JavaScript plus PHP in the clickCheckbox.phpfile, say something like this:

你可以在clickCheckbox.php文件中编写 JavaScript 和 PHP ,这样说:

clickCheckbox.php

clickCheckbox.php

<?php
    header("Content-type: text/javascript");
    unlink("delete.png");
    echo 'alert("Deleted!");';
?>

Once you click on the checkbox, and if the state is checked, it gives out an AJAX call to this PHP file, where you are deleting a file delete.pngand in the echostatement, you are outputting a JavaScript alert, so that you will get an alert message saying Deleted!.

单击复选框后,如果状态为checked,它会向此 PHP 文件发出 AJAX 调用,您将在其中删除文件,delete.png并在echo语句中输出 JavaScript 警报,以便您收到警报消息说Deleted!

回答by Keith A

use

if ($('#checkbox').is(':checked'))

or inside an event

或在事件内

$('#checkbox').click(function(){

 if ($(this).is(':checked')){
    //your routine here if checked
 }else{
    //routine here if not checked
 }
});

回答by Nilesh Gupta

Try this one

试试这个

<input id="checkbox" name="click" type="checkbox" onclick="check()"/>

//in js
if( $('input[name=checkbox]').is(':checked') ){
   // your code
}