I would like to know how to pass the value of a Radius via post with ajax or jquery

Asked

Viewed 97 times

1

I needed help to pass any value of one of these inputs via Ajax or jQuery:

<input type="radio" id="valorum" name="valor" value="38,70" ></br>
<input type="radio" id="valordois" name="valor" value="71,40" ></br>
<input type="radio" id="valortres" name="valor" value="118,80" ><br><br>


$.ajax({
  method: "GET",
  url: "test.js",
  dataType: "script"
});

How would I pass one of these values inside this Ajax? Or there is no way, or there is another method to do it?

2 answers

1

Take the values by id.

<script>
var value = $("#valorum").val();
$.ajax({
        method: "GET",
        data: { suaVariavelDoServidor: value },
        url: "test.js",
        dataType: "script"
});
</script>

If you want to send all values as an array:

var values = []
$("input").each(function(){
     values.push(this.value);   
});
$.ajax({
        method: "GET",
        data: { suaVariavelDoServidor: values },
        url: "test.js",
        dataType: "script"
});
  • The camps already had one id each. :)

  • But the script works ;)

  • Wouldn’t it be better to edit the answer?

  • yes would be. : - )

  • Hmm, I think you understand!! In case the value of Radius will be sent inside the url:test.js ?

1

You can use this line to take the value of the button that is selected, replace the ID myForm by the ID of your form

Example of how the code should look

var campoRadio =  $('input[name=valor]:checked', '#myForm').val();
$.ajax({
   method: "GET",
   data: { campoRadio: campoRadio },
   url: "test.js",
   dataType: "script"
});

The ideal would be if you could use the post in place of get

  • Marcos, I didn’t even submit a form in this code. Just by clicking on Radius I wouldn’t be able to send its value to test.js ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.