我需要你的帮助我卡住,因为当我点击第一个按钮它将执行但另一个按钮它不会执行我使用ajax表格,我使用foreach。如果有人知道如何解决,请告诉我。
这是我目前的观看代码:
<tbody>
@foreach($customers as $name => $member)
@foreach($member->slice(0,1) as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->MATNR}}</td>
<td>{{$value->MAKTX}}</td>
<td>
<select class="form-control">
<option>{{$value->UNIT1}}({{$value->CONV1}})</option>
<option>{{$value->UNIT2}}({{$value->CONV2}})</option>
</select>
</td>
<td>
<select class="form-control">
@foreach($member as $value)
<option value="{{$value->CHARG}}">{{$value->CHARG}}</option>
@endforeach
</select>
</td>
<td>{{$value->VERME}}</td>
<td>
<form name="ajaxForm" id="ajaxForm" class="form-horizontal" novalidate="">
<input type="text" name="_token" value="{{ csrf_token() }}">
<input type="text" name="id" value="{{$value->id}}">
<button type="button" class="btn btn-primary" id="btnSave" value="add">Save</button>
</form>
</td>
</tr>
@endforeach
@endforeach
</tbody>
这是我当前的控制器代码
public function cart() {
if (Request::isMethod('post')) {
$id = Request::get('id');
$customers = DB::table('Materials')->select('Materials.MATNR','Materials.MAGRV','Materials.CONV1','Materials.id','Materials.CONV1','Materials.UNIT1','Materials.CONV2','Materials.UNIT2','Materials.MHDHB','Materials.MAKTX','Inventory.CHARG','Inventory.VERME')->join('Inventory',function($join){
$join->on('Inventory.MATNR','LIKE',DB::raw("CONCAT('%',Materials.MATNR,'%')"));
})->groupBy('Materials.MATNR','Materials.MAGRV','Materials.CONV1','Materials.id','Materials.CONV1','Materials.UNIT1','Materials.CONV2','Materials.UNIT2','Materials.MHDHB','Materials.MAKTX','Inventory.CHARG','Inventory.VERME')->find($id);
$cart = ShoppingCart::add($customers->id, $customers->CHARG, 5, 100.00, ['MAKTX' => $customers->MAKTX, 'kg' => $customers->VERME]);
}
}
我目前的路线
Route::post('/cart', 'system\request_customer@cart');
$(document).ready(function(){
$("#btnSave").click(function (e) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
})
e.preventDefault();
$.ajax({
type: 'POST',
url: 'cart',
data: $('#ajaxForm').serialize(),
dataType: 'json',
success: function (data) {
console.log(data);
},
error: function (data) {
console.log('Error:', data);
}
});
});
});
答案 0 :(得分:1)
<form method="POST" class="form-horizontal ajaxForm">
<input type="hidden" id="token" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" id="id" value="{{$value->id}}">
<input type="hidden" name="MATNR" value="{{$value->MATNR}}">
<button>Submit</button>
</form>
$(".ajaxForm").submit(function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
url: 'cart',
type: 'POST',
data: formData,
success: function (data) {
console.log(data)
},
cache: false,
contentType: false,
processData: false
});
});
答案 1 :(得分:0)
我遇到了同样的问题。我用id
替换class
解决了这个问题。我不知道为什么这是一个解决方案,但它适用于我的情况。为了方便您,您需要做的就是这些。试试吧,据我说,它必须有效!
#ajaxForm
替换为.ajaxForm
。<form name="ajaxForm" id="ajaxForm" class="form-horizontal" novalidate="">
替换为<form name="ajaxForm" class="form-horizontal ajaxForm" novalidate="">
让我知道它是否有效。它适用于我的情况。就我而言,我遇到了同样的错误。我的意思是,只有第一个按钮或提交按钮有效。
如果您是JQuery
专家或了解其背后的原因,请告诉我们!