Get value from a radius in jquery cakephp

2

In my code I have a radio and would like to get the selected option in a variable in my jquery function.

Button Input:

    <?php
        echo $this->Form->input('attendance_status_id', array(
          'type' => 'radio',
          'options' => $attendance_statuses,
          'div' => array('id' => 'attendance-statuses', 'class' => 'attendance_statuses'),
          'legend' => '',
          'label' => true
          ));
          ?>

Script where I want to receive it:

    $("#attendance-statuses").change(function(){
  var obs = $('input[name = attendance_status_id]:checked').serialize();
  console.log(obs);
  $.ajax({
    type: 'post',
    data: 
    {obs, 
    obsEntity: 1
    },
    url: "<?php echo Router::url('/observations/observation_options'); ?>",
    success:function(dados){
      dados = JSON.parse(dados);
      var $el = $("#obs-select");
      $('#obs-select option:gt(0)').remove();
      $.each(dados, function(value,key) {
        $el.append($("<option></option>")
           .attr("value", value).text(key));
      });
    }
  });
});
    
asked by anonymous 09.04.2015 / 23:14

1 answer

1

Notice that this object is poorly formatted:

data: {
    obs, 
    obsEntity: 1
},

should be

data: {
    obs: obs,   // <-- faltava-te "valor" dessa "chave" do par chave/valor
    obsEntity: 1
},

Since radio buttons can only be chosen one at a time you could also have:

// usei ".val()" em vez de ".serialize()"
var obs = $('input[name = attendance_status_id]:checked').val(); 

and in AJAX:

data: {
    attendance_status_id: obs, 
    obsEntity: 1
},

But it's a matter of preference and I do not even know how you're doing in PHP to receive this POST.

    
10.04.2015 / 07:33