Reputation: 320
When the link is clicked it should pop up the dialog box when the download later button is clicked it toggles the checked variable of the button. Then closes the dialog button
I am new to Javascript and Jquery and have no idea what I am doing
$(function download_box(checkbox) {
// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
$( "#opener" ).click(function() {
$( "#dialog-download" ).dialog( "open" );
return false;
});
$( "#dialog-download" ).dialog({
autoOpen: false,
resizable: false,
height:140,
width:325,
modal: true,
buttons: {
"Download Now": function() {
var mycheckbox1 = document.getElementById(checkbox);
if(mycheckbox1.checked){
box.checked=false;
}
else{
box.checked=true;
}
$( this ).dialog( "close" );
},
"Download Later": function() {
$('#c2').prop("checked", true);
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
<form method="post" action="<?php echo $PHP_SELF;?>">
<a id="opener" href="#" OnClick="download_box('#c1')">db1.csv:</a>
<input id="c1" type="checkbox" name="download[]" value="db1.csv" />
<!-- ... -->
<input type="submit" value="submit" name="submit">
</form>
Upvotes: 0
Views: 355
Reputation: 94459
I gave all of your anchor tags a class of opener, then I bound a click function to that anchor that sets the options on the dialog. The options set are mainly the buttons you would like to have on the form and their corresponding functions. I used this method because the functions needed to be slightly dynamic therefore I couldn't declare them when initially creating the dialog. The button toggles the checkbox, the download later button clicks the checkbox and closes the dialog and finally the close button simply closes the dialog.
Some things to note, there should only be 1 element with a given id per page, so having 9 anchor tags with an id of opener is asking for trouble. Also using jquery it is very easy to bind functions to events on html elements, this is a preferred method over attaching the functions inline in the html ie (onclick=download_box).
HTML
<div id="dialog-download" title="Download Now?">
<p><span style="float:left; margin:0 7px 20px 0;"></span>Download the file now or later?</p>
</div>
<form method="post" action="<?php echo $PHP_SELF;?>">
<a class="opener" href="#">db1.csv:</a>
<input id="c1" type="checkbox" name="download[]" value="db1.csv" /><br />
<a class="opener" href="#" >db2.csv:</a>
<input id="c2" type="checkbox" name="download[]" value="db2.csv" /><br />
<a class="opener" href="#" >db3.csv:</a>
<input id="c3" type="checkbox" name="download[]" value="db3.csv" /><br />
<a class="opener" href="#" >db4.csv:</a>
<input id="c4" type="checkbox" name="download[]" value="db4.csv" /><br />
<a class="opener" href="#" >db5.csv:</a>
<input id="c5" type="checkbox" name="download[]" value="db5.csv" /><br />
<a class="opener" href="#" >db6.csv:</a>
<input id="c6" type="checkbox" name="download[]" value="db6.csv" /><br />
<a id="opener" href="#">db7.csv:</a>
<input id="c7" type="checkbox" name="download[]" value="db7.csv" /><br />
<a class="opener" href="#">db8.csv:</a>
<input id="c8" type="checkbox" name="download[]" value="db8.csv" /><br />
<a class="opener" href="#" >db9.csv:</a>
<input id="c9" type="checkbox" name="download[]" value="db9.csv" /><br />
<input type="submit" value="submit" name="submit">
</form>
Javascript
$(document).ready(function(){
$("#dialog-download ").dialog({
autoOpen: false,
resizable: false,
height: 140,
width: 325,
modal: true
});
$(".opener").click(function(){
var that = this;
var checkbox = $(that).next(":checkbox");
$("#dialog-download").dialog("option", {
buttons: {
"Download Now": function(){
$(checkbox).prop("checked", !$(checkbox).attr("checked"));
$("#dialog-download").dialog("close");
},
"Download Later ": function(){
$(checkbox).prop("checked", true);
$("#dialog-download").dialog("close");
},
"Cancel": function(){
$("#dialog-download").dialog("close");
}
}
});
$("#dialog-download").dialog("open");
});
});
Working Example: http://jsfiddle.net/ccrAF/
Upvotes: 1