簡體   English   中英

在PHP的JavaScript中獲取Checkbox []值

[英]Get Checkbox[] values in javascript for php

我在獲取復選框中的值時遇到問題。 我試圖清理代碼。

我已經能夠選擇所有選中的復選框(不是每行),或者我可以獲取每行的復選框ID,但沒有選中的值。 我很確定我可以將值傳輸到另一個頁面,但不知道如何獲取它們。

在這里的任何幫助將不勝感激!

 //php variables used
 $pages = 2
 $size = 4 
  <form id="form" name="cb">
  <div style=" width:800px; height:500px; overflow:auto">
  <h2>Select Editions</h2>
  <table id='table' name='table' border=1 cellpadding=7 width=50% height=50% align='center'>\n
    for($x = 1; $x <= $pages; $x++) :
      print "<td id='page_$x' class='page_button' align='center' custom='0' >Page $x - ";
        for($i = 1; $i <= $size; $i++) :
          print "<input type='checkbox' class='ebutton' id='etype_$x' name='checks[]' value='$i' /> $i";
        endfor;
      </td>
      </tr>
    //php end for loop
    endfor;
  </div>
  </form>

這是根據復選框類的類更新的Javascript

    $(document).ready(function() {
      $(".ebutton").change(function() {

        var idp = $(this).attr("id").split("_");
        var page_num = idp[1];

        //  I need to find out how to get the checkboxes that are checked per row. Ex: 01,02
        //var editions = ?;
        //alert(editions);

        var hidden_id = "#etype_page_" + page_num;
          if($(hidden_id).length < 1) {
            $("#base").append('<input type="hidden" id="etype_page_'+ page_num +'" name="'+ page_num +'"  value="'+ editions +'" class="hidden_edtype" custom="' + editions +'">');
          } else {
            $(hidden_id).val($(this).val());
          }
        update_eShow();
      });
    });

    function update_eShow() {
      $("#eShow").html('');
        $(".hidden_edtype").each(function() {
         var page = $(this).attr("name");
         var value = $(this).attr("custom");
         $("#eShow").append('page:' + page + ' values:' + value +'<br>');
      });
    }

頁面看起來像這樣:

| 第1頁-[] 1 [] 2 [] 3 [] 4 |

| 第2頁-[] 1 [] 2 [] 3 [] 4 |

名稱應具有不同的編號,以與您的ID相同的唯一形式發送名稱。

嘗試設置類似id的名稱: name='etype_$x'

然后使用調試控制台(如果使用的是chrome,請按f12鍵),然后進入網絡並可視化表單中發送的變量和值。

var $editions = $("#page_"+page_num+" input[checked=checked]");
var editions = [];
$editions.each(function(input) {
  editions.push($(input).val());
});

您不需要JavaScript即可將復選框發送到php

您可以將支票命名為checks checks[$page][]

print "<input type='checkbox' class='ebutton' id='etype_$x' name='checks[$x][]' value='$i' /> $i";

並且在您的操作頁面中,您將在$ _POST(pr $ _GET)[“ checks”] [1]中包含一個數組,其中包含已為頁面1檢查的值(如果有)

現在,知道是一個彈出窗口將支票發送給父母...

進行如下檢查:

print "<input type='checkbox' class='ebutton checks$x' id='etype_$x' name='checks".$x."[]' value='$i' /> $i";

要獲得頁面1的支票類別為“ checks1”,頁面2的支票類別為“ checks2”,...

並獲得檢查的:

 var page_num = idp[1];
 var checks=$('checks'+page_num).attr('checked',true); //to get all checked checkboxes of the class checks+page_num
 var editions="";
 checks.each(function(ch) {
     editions+=$(ch).val()+',';
 }

和版本將該頁面的所有選中值都用','分隔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM