简体   繁体   中英

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

        <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

how to send data like key(id entity) => value(value check box) ?

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 .

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

And pass it to ajax data like this:

data:dictionary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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