[英]Check box ajax data
我有复选框,想要发送值复选框(0或1),并在单击复选框的实体上执行某些操作
<input class="searchType" type="checkbox" name="SharingNotification" id={{ taskExecution.id }}>
<label class="searchtype2label">{{ taskExecution.id }}</label>
</input>
$('.searchType').click(function(event) {
if(this.checked){
$.ajax({
type: "POST",
url: '/app_dev.php/api/customer/customers',
data: {
id: event.target.id,
value: event.target.value
},,
success: function(data) {
alert('it worked');
alert(data);
$('#container').html(data);
},
error: function() {
alert('it broke');
},
complete: function() {
alert('it completed');
}
});
}
});
在请求中
request = {Symfony\Component\HttpFoundation\ParameterBag} [1]
parameters = {array} [2]
id = "1"
value = "on"
我试着这样
event.target.id
"1"
event.target.value
"on"
但为什么“on”我认为可以是1或0
如何发送像key(id entity)=> value(value复选框)这样的数据?
您可以像这样创建要发送的对象:
var objToSend = {};
objToSend[$(this).attr('id')] = $(this).is(":checked");
并发送你的数据
data: objToSend
在这种情况下,您应该使用dictionary
并将其发送到code-behind
。
var dictionary= {};
dictionary[$(this).attr('id')] = $(this).is(":checked")==true? 1:0;
并将其传递给这样的ajax
数据:
data:dictionary
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.