如何获取复选框值并将其发送到后端?

时间:2016-01-05 04:21:08

标签: javascript jquery html5 nodes ejs

这是前端,

<div class="uk-width-1-4 ">
   <table class="uk-table uk-table-striped uk-table-hover" id="tabledata">
      <thead>
         <tr>
            <th><input type="checkbox"  ></th>
            <th>Site ID</th>
         </tr>
      </thead>
      <tbody>
         <tr>
            <td>
               <input type="checkbox" id="chkall"  value="<%= d.siteid%>">
            </td>
            <td><%=d.siteid%></td>
         </tr>
         <%  });
            } %>         
      </tbody>
   </table>
</div>

这是后端,

   var user_id=req.query.user_id; 
   var siteid=req.query.chkall;
   console.log("chkall =="+siteid);
    sql="UPDATE site SET userid='"+user_id+"' WHERE siteid IN('"+siteid+"')";
  }
  getSQLData(sql, function(resultsets){
      console.log("user...sql"+sql);
       userresults = resultsets;
  });

5 个答案:

答案 0 :(得分:0)

你可以像这样使用它:

$("input[type='checkbox']").val();
希望能帮到你。

答案 1 :(得分:0)

如果您正在进行表单提交,并且如果未选中复选框,则false值将不会转到后端。如果未选中复选框,则应该有一些隐藏值。

如果是ajax调用,您可以获得如下所示的复选框值

$("input[type='checkbox']").val();

or 

$("#checkboxid").val();

答案 2 :(得分:0)

如果您提交表单,请写名称标签。如果你从ajax发送数据然后按元素id获取值,则发送到服务器。

答案 3 :(得分:0)

你喜欢这个..

<?php require_once('Connections/greatCONN.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
  $insertSQL = sprintf("INSERT INTO great (id) VALUES (%s)",
                       GetSQLValueString($_POST['fileToUpload'], "int"));

  mysql_select_db($database_greatCONN, $greatCONN);
  $Result1 = mysql_query($insertSQL, $greatCONN) or die(mysql_error());
}
?>
<!DOCTYPE html>
<html>
<body>

<form name="form" action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
    <input type="hidden" name="MM_insert" value="form">
</form>

</body>
</html>

如果你有列表,那么执行循环并添加到全局变量并使用该变量。

答案 4 :(得分:0)

//复选框

<asp:CheckBox ID="chkpermission" runat="server" onclick="checking(this.id);" Checked='<%# Convert.ToBoolean(Eval("permission")) %>' />

<script type="text/javascript">
        function checking(id) {
            $('#ctl00_ContentPlaceHolder1_lblresponce').val('');
            if (id) {
                ids = id.split('_');
                var chkval = document.getElementById(id).checked;
                var TitleId = "ctl00_ContentPlaceHolder1_grdpermissions_" + ids[3] + "_hdntitleid"; //here i am getting the value from the grid

                var pagePath = window.location.pathname;
                var param = JSON.stringify({ "TitleId": itleId,"chkval":chkval });
                $.ajax({
                    type: "POST",
                    url: pagePath + "/updatevalue",
                    data: param,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        if (data != null) {
                            var result = data.d;
                            $('#ctl00_ContentPlaceHolder1_lblresponce').text('Updated successfully');

                        }
                    },
                    error: function () {
                        var r = jQuery.parseJSON(response.responseText);
                        alert("Message: " + r.Message);
                        alert("StackTrace: " + r.StackTrace);
                        alert("ExceptionType: " + r.ExceptionType);
                    }
                });
            }


        }
    </script>

在后端代码中

[WebMethod]
    public static Boolean updatevalue( string TitleId,  string chkval)
    {
       Boolean flag = false;
        if ( TitleId != "" &&  chkval !="")
        {
          //Update your your query here with single id...

flag=true;
// return flag if updated
        }



        return flag;
    }

我希望它适用于您的情况。