vendredi 9 janvier 2015

jQuery running at the same time when page loading

I need your help. I have a jquery code which is working nice after I first load the page and as a second step check the checkbox. In background function updateSg(opts) invokes the submit.php which results a Json.


But I would like to solve when I open the page, the checkbox input tag gets the checked value from URL, and the checkbox is checked but the jquery code is not run. I have to uncheck and check again for the code running.


Could you give me a help that the code is executed with the page loading together.


Thanks, Atti



<div id="result"></div>
<div id="filter">
<h2>Filter</h2>
<div>
<input type="checkbox" id="sg" name="sg" checked>
<label for="sg">Something</label>
</div>
</div>
<script src="http://ift.tt/rs8qB8"></script>

<script>
var getsg = "something";

function makeTable(data){
var tbl_body = "";
$.each(data, function(k,v) {
tbl_body += getsg;
})
return tbl_body;
}

function getSgFilterOptions(){
var opts = [];
$checkboxes.each(function(){
if(this.checked){
opts.push(this.name);
}
});

return opts;
}

function updateSg(opts){
$.ajax({
type: "POST",
url: "submit.php",
dataType : 'json',
cache: false,
data: {filterOpts: opts},
success: function(records){
$('div#result').html(makeTable(records));
}
});
}

var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function(){
var opts = getSgFilterOptions();
updateSg(opts);
});

updateSg();

</script>




Aucun commentaire:

Enregistrer un commentaire