2016-05-01 55 views
1

如何獲取並提交購物車中的選定項目?如何獲取並提交購物車中的選定項目?

下面是我的演示,
有2個問題:
1,目前,我只能得到一個手工項目,如何讓所有選定的項目(S)?
2,如果沒有選擇任何東西,我希望「提交」按鈕被禁用。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet"> 
    <link href="https://cdn.bootcss.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type='text/css'> 
    <link href="https://cdn.bootcss.com/tether/1.2.0/css/tether.min.css" rel="stylesheet"> 
    <link href="https://cdn.bootcss.com/tether/1.2.0/css/tether-theme-basic.min.css" rel="stylesheet"> 
</head> 
<body> 
<div class="container" id="goodsTable"> 
    <table class="table"> 
     <thead> 
     <tr> 
      <th class="col-xs-3"> 
       <label class="c-input c-checkbox"> 
        <input type="checkbox" name="selectAll" id="selectAll"> 
        <span class="c-indicator"></span>Select All 
       </label> 
      </th> 
      <th class="col-xs-5">Name</th> 
      <th class="col-xs-4">Number</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr> 
      <td class="col-xs-3"> 
       <label class="c-input c-checkbox"> 
        <input type="checkbox" name="domainList" id="goods_100" class="checkboxSelection"> 
        <span class="c-indicator"></span> 
       </label> 
      </td> 
      <td class="col-xs-5">Apple</td> 
      <td class="col-xs-4"><input type="text" class="form-control" id="number_100" value="1000"></td> 
     </tr> 
     <tr> 
      <td class="col-xs-3"> 
       <label class="c-input c-checkbox"> 
        <input type="checkbox" name="domainList" id="goods_101" class="checkboxSelection"> 
        <span class="c-indicator"></span> 
       </label> 
      </td> 
      <td class="col-xs-5">Pear</td> 
      <td class="col-xs-4"><input type="text" class="form-control" id="number_101" value="1200"></td> 
     </tr> 
     <tr> 
      <td class="col-xs-3"> 
       <label class="c-input c-checkbox"> 
        <input type="checkbox" name="domainList" id="goods_102" class="checkboxSelection"> 
        <span class="c-indicator"></span> 
       </label> 
      </td> 
      <td class="col-xs-5">Peach</td> 
      <td class="col-xs-4"><input type="text" class="form-control" id="number_102" value="1500"></td> 
     </tr> 
     </tbody> 
    </table> 
    <button type="button" id="submit" class="btn btn-primary">Submit</button> 
</div> 
<script src="https://cdn.bootcss.com/jquery/2.2.1/jquery.min.js"></script> 
<script src="https://cdn.bootcss.com/tether/1.2.0/js/tether.min.js"></script> 
<script src="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script> 
<script> 
    //Select all 
    $("#selectAll").change(function() { 
     if ($(this).is(":checked")) { 
      $(".checkboxSelection").each(function() { 
       $(this).prop('checked', true); 
      }); 
     } 
     else { 
      $(".checkboxSelection").each(function() { 
       $(this).prop('checked', false); 
      }); 
     } 
    }); 

    $(".checkboxSelection").change(function() { 
     var allSelected = true; 
     $(".checkboxSelection").each(function() { 
      if (!$(this).is(":checked")) { 
       $("#selectAll").prop('checked', false); 
       allSelected = false; 
      } 
     }); 
     if (allSelected) 
      $("#selectAll").prop('checked', true); 
    }); 
</script> 


<script> 
    //Get the value and submit 
    $(function() { 
     //Below is getting one of the items,how to get all the selected item(s) ? 
     var data = { 
      "name": $("#goods_100").val(), 
      "number": $("#number_100").val() 
     }; 

     //ajax submit 
     $(document).on('submit', '#goodsTable', function (e) { 
      e.preventDefault(); 
      $("#submit").addClass('disabled'); 

      $.ajaxSetup({ 
       headers: { 
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
       } 
      }); 

      $.ajax({ 
         type: "POST", 
         url: "{{ url('/goods/') }}", 
         cache: false, 
         data: data, 
         dataType: "json" 
        }) 
        .done(function (msg) { 
         window.location.href = "/goods/success"; 
        }) 
        .fail(function (textStatus) { 
         $("#submit").removeClass('disabled'); 
         alert('Fail,try again'); 
        }); 
     }); 
    }); 
</script> 
</body> 
</html> 

回答

2

每一個你的複選框具有相同的名稱,因此發生的事情是:

domainList = on
domainList = on
domainList = on
domainList = on

而且只有最後一個被髮送。您需要將[]添加到名稱以使其成爲數組。

domainList[] = on
domainList[] = on
domainList[] = on
domainList[] = on

這將使所述Laravel控制器的陣列,而不是僅僅一個。

例子:

<input type="checkbox" name="domainList[]" id="goods_102" class="checkboxSelection"> 

但是,你正在使用這些複選框。他們有一個默認值「開」,並且只在檢查時顯示在表單數據中。你一定要添加一個代表項目數據庫行的value

例如:

<input type="checkbox" name="domainList[]" id="goods_102" class="checkboxSelection" value="stackoverflow.com" > 

這將返回一個字符串

domainList[] = stackoverflow.com
domainList[] = stackexchange.com
domainList[] = serverfault.com

+0

Thanks!和javascript代碼:var data = {name}:$(「#goods_100」)。val(), 「number」:$(「#number_100」)。val() };''' '如何寫在這裏? – zwl1619

+0

你不需要那樣做。把你的表放在''form id =「myform」>'中,然後設置'var data = $(「#myForm」)。serialize();'和jQuery完成剩下的工作。 – Josh

+0

https://api.jquery.com/serialize/ – Josh

2

更新你的腳本與下面的第二個問題組成的數組:

$(document).ready(function(){ 
 
\t if($(".table tr input[type='checkbox']:checked").length > 0) 
 
\t { 
 
     $('#submit').attr("disabled",false); 
 
\t } 
 
\t else 
 
\t { 
 
     $('#submit').attr("disabled","disabled"); 
 
\t } 
 

 
    //Select all 
 
    $("#selectAll").change(function() { 
 
     if ($(this).is(":checked")) { 
 
      $(".checkboxSelection").each(function() { 
 
       $(this).prop('checked', true); 
 
      }); 
 
\t \t $('#submit').attr("disabled",false); 
 
     } 
 
     else { 
 
      $(".checkboxSelection").each(function() { 
 
       $(this).prop('checked', false); 
 
      }); 
 
\t \t $('#submit').attr("disabled","disabled"); 
 
     } 
 
    }); 
 

 
    $(".checkboxSelection").change(function() { 
 
     var allSelected = true; 
 
     $(".checkboxSelection").each(function() { 
 
      if (!$(this).is(":checked")) { 
 
       $("#selectAll").prop('checked', false); 
 
       allSelected = false; 
 
      } 
 
     }); 
 
     if (allSelected) 
 
\t  { 
 
      $("#selectAll").prop('checked', true); 
 
\t  } 
 
\t  if($(".table tr input[type='checkbox']:checked").length < 1) 
 
\t  { 
 
\t \t $('#submit').attr("disabled","disabled"); 
 
\t  } 
 
\t  else 
 
\t  { 
 
\t \t \t $('#submit').attr("disabled",false); 
 
\t  } 
 
    }); 
 
});
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet"> 
 
    <link href="https://cdn.bootcss.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type='text/css'> 
 
    <link href="https://cdn.bootcss.com/tether/1.2.0/css/tether.min.css" rel="stylesheet"> 
 
    <link href="https://cdn.bootcss.com/tether/1.2.0/css/tether-theme-basic.min.css" rel="stylesheet"> 
 
</head> 
 
<body> 
 
<div class="container" id="goodsTable"> 
 
    <table class="table"> 
 
     <thead> 
 
     <tr> 
 
      <th class="col-xs-3"> 
 
       <label class="c-input c-checkbox"> 
 
        <input type="checkbox" name="selectAll" id="selectAll"> 
 
        <span class="c-indicator"></span>Select All 
 
       </label> 
 
      </th> 
 
      <th class="col-xs-5">Name</th> 
 
      <th class="col-xs-4">Number</th> 
 
     </tr> 
 
     </thead> 
 
     <tbody> 
 
     <tr> 
 
      <td class="col-xs-3"> 
 
       <label class="c-input c-checkbox"> 
 
        <input type="checkbox" name="domainList" id="goods_100" class="checkboxSelection"> 
 
        <span class="c-indicator"></span> 
 
       </label> 
 
      </td> 
 
      <td class="col-xs-5">Apple</td> 
 
      <td class="col-xs-4"><input type="text" class="form-control" id="number_100" value="1000"></td> 
 
     </tr> 
 
     <tr> 
 
      <td class="col-xs-3"> 
 
       <label class="c-input c-checkbox"> 
 
        <input type="checkbox" name="domainList" id="goods_101" class="checkboxSelection"> 
 
        <span class="c-indicator"></span> 
 
       </label> 
 
      </td> 
 
      <td class="col-xs-5">Pear</td> 
 
      <td class="col-xs-4"><input type="text" class="form-control" id="number_101" value="1200"></td> 
 
     </tr> 
 
     <tr> 
 
      <td class="col-xs-3"> 
 
       <label class="c-input c-checkbox"> 
 
        <input type="checkbox" name="domainList" id="goods_102" class="checkboxSelection"> 
 
        <span class="c-indicator"></span> 
 
       </label> 
 
      </td> 
 
      <td class="col-xs-5">Peach</td> 
 
      <td class="col-xs-4"><input type="text" class="form-control" id="number_102" value="1500"></td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
    <button type="button" id="submit" class="btn btn-primary">Submit</button> 
 
</div> 
 
<script src="https://cdn.bootcss.com/jquery/2.2.1/jquery.min.js"></script> 
 
<script src="https://cdn.bootcss.com/tether/1.2.0/js/tether.min.js"></script> 
 
<script src="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script>

+0

謝謝,沒關係,但是,打開時,什麼也沒有完成,它已啓用。根據您的要求更新代碼 – zwl1619

+0

。 – Ronak

+0

好的,再次感謝! – zwl1619

相關問題