Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
143
rated 0 times [  150] [ 7]  / answers: 1 / hits: 5262  / 10 Years ago, mon, may 19, 2014, 12:00:00

I am trying to send a checkbox value via ajax to send data to a external function.



I have the function working using the following javascript, however it is very messy as i am currently writing this javascript function for every instance of script which exisits.



<form>
<input type=checkbox name=event_status id=event_status1455 value=1 <?php echo ($event_status == '1' ? 'checked=checked' : '') ?>>
</form>

<form>
<input type=checkbox name=event_status id=event_status1456 value=1 <?php echo ($event_status == '1' ? 'checked=checked' : '') ?>>
</form>

<form>
<input type=checkbox name=event_status id=event_status1457 value=1 <?php echo ($event_status == '1' ? 'checked=checked' : '') ?>>
</form>

<script>
$('input:checkbox').change(function(e) {
e.preventDefault();
var isChecked = $(input:checkbox).is(:checked) ? 1:0;
$.ajax({
type: 'POST',
url: '<?php echo $this->url('/profile/list', 'event_status', $cobj->getCollectionID())?>',
data: { event_status:$(input:checkbox).attr(id), event_status:isChecked }
});
});
</script>


As you can see above the javascript will only respond to this particular checkbox. i cannot figure out how to code it so that i have one javascript function on my page that will run for each checkbox i have.



I Have tweaked the code from my original post. I have my ajax doing what i want, however the ajax function only executes for the last checkbox, is their away i can transform the javascript to to do provide the function for each checkbox?


More From » ajax

 Answers
6

solved my problem i need to grab the id of my checkbox in order to distinguish which checkbox value i was running my function on.



<script>
$('input:checkbox').change(function(e) {
e.preventDefault();

// Determine ID
var value = $(this).attr('id');
var EsID = value;

var isChecked = $(input:checkbox).is(:checked) ? 1:0;
$.ajax({
type: 'POST',
url: <?php echo $this->url('/profile/list', 'event_status')?>+EsID +,
data: { event_status:$(input:checkbox).attr(id), event_status:isChecked }
});
});
</script>

[#45208] Sunday, May 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hadens

Total Points: 142
Total Questions: 98
Total Answers: 100

Location: Kenya
Member since Mon, Jun 14, 2021
3 Years ago
;