简体   繁体   English

复选框ajax数据

[英]Check box ajax data

I have check box and want send value check box (0 or 1) and in entity where clicked check box on some action 我有复选框,想要发送值复选框(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');
            }
        });

    }
});

in request in action 在请求中

request = {Symfony\Component\HttpFoundation\ParameterBag} [1]
 parameters = {array} [2]
  id = "1"
  value = "on"

I try like this 我试着这样

event.target.id
"1"
event.target.value
"on"

but why "on" I think can be 1 or 0 但为什么“on”我认为可以是1或0

how to send data like key(id entity) => value(value check box) ? 如何发送像key(id entity)=> value(value复选框)这样的数据?

You can create the object to send like this : 您可以像这样创建要发送的对象:

var objToSend = {};
objToSend[$(this).attr('id')] = $(this).is(":checked");

And send your data like 并发送你的数据

data: objToSend 

In this case you should use a dictionary and send it to code-behind . 在这种情况下,您应该使用dictionary并将其发送到code-behind

var dictionary= {};
dictionary[$(this).attr('id')] = $(this).is(":checked")==true? 1:0;

And pass it to ajax data like this: 并将其传递给这样的ajax数据:

data:dictionary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM