Laravel getting value of checkbox in ajax Error :Cannot read property 'checked' of null
Laravel getting value of checkbox in ajax Error :Cannot read property 'checked' of null
当用户尝试将其更改为未选中并更新值时,我会根据数据库 value.but 显示选中或未选中的复选框值。我打电话给 ajax 我收到这个错误。
感谢帮助
代码
<div class="{{count($employees) !=0 ? 'sidebar-hide': 'sidebar-customize'}}">
<a>
{{ Form::checkbox('Employee','1',$user->employee) }}
<i class="fa fa-id-card"></i>
<span>Employee</span>
</a>
</div>
下面是Ajax函数
$(document).ready(function(){
$('#customize').click(function(e){
e.preventDefault();
var customers=document.getElementById('customers').checked ? '1' :'0' ;
var accounts=document.getElementById('accounts').checked ? '1' :'0';
var Inventory=document.getElementById('InventoryItems').checked ? '1' :'0';
var Employee=document.getElementsByName('Employee').checked ? '1' :'0';
console.log(Employee);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url :"{{ url('user')}}",
type :'POST',
data :{
customer : customers,
accounts : accounts,
inventory: Inventory,
employee : Employee
},
dataType: 'JSON'
// ,
// success: function( data ) {
// $("#ajaxResponse").append(data.msg);
// console.log(data);
//}
});
});
});
HTML是动态创建的吗?
如果是这样,那么您需要将事件绑定到父元素并使用 jQuery.on() 方法而不是 click()
$('#parentelement').on('click', 'div.sidebar-customize', function(e) {
//code
});
该方法的第二个参数需要以您尝试使用的元素为目标。你HTML,我不知道那最终会是什么。也许实施额外的 class 或 ID,以便更好地定位 HTML。
好像我必须包含复选框中的 ID
{{ Form::checkbox('Employee', 'Employee', $user->employee, ['id' => 'employee']) }}
Ajax代码:
$(document).ready(function(){
$('#customize').click(function(e){
e.preventDefault();
var employee=document.getElementById('employee').checked ? '1' :'0' ;
console.log(employee);
});
});
各方面都很好
当用户尝试将其更改为未选中并更新值时,我会根据数据库 value.but 显示选中或未选中的复选框值。我打电话给 ajax 我收到这个错误。
感谢帮助 代码
<div class="{{count($employees) !=0 ? 'sidebar-hide': 'sidebar-customize'}}">
<a>
{{ Form::checkbox('Employee','1',$user->employee) }}
<i class="fa fa-id-card"></i>
<span>Employee</span>
</a>
</div>
下面是Ajax函数
$(document).ready(function(){
$('#customize').click(function(e){
e.preventDefault();
var customers=document.getElementById('customers').checked ? '1' :'0' ;
var accounts=document.getElementById('accounts').checked ? '1' :'0';
var Inventory=document.getElementById('InventoryItems').checked ? '1' :'0';
var Employee=document.getElementsByName('Employee').checked ? '1' :'0';
console.log(Employee);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url :"{{ url('user')}}",
type :'POST',
data :{
customer : customers,
accounts : accounts,
inventory: Inventory,
employee : Employee
},
dataType: 'JSON'
// ,
// success: function( data ) {
// $("#ajaxResponse").append(data.msg);
// console.log(data);
//}
});
});
});
HTML是动态创建的吗?
如果是这样,那么您需要将事件绑定到父元素并使用 jQuery.on() 方法而不是 click()
$('#parentelement').on('click', 'div.sidebar-customize', function(e) {
//code
});
该方法的第二个参数需要以您尝试使用的元素为目标。你HTML,我不知道那最终会是什么。也许实施额外的 class 或 ID,以便更好地定位 HTML。
好像我必须包含复选框中的 ID
{{ Form::checkbox('Employee', 'Employee', $user->employee, ['id' => 'employee']) }}
Ajax代码:
$(document).ready(function(){
$('#customize').click(function(e){
e.preventDefault();
var employee=document.getElementById('employee').checked ? '1' :'0' ;
console.log(employee);
});
});
各方面都很好