pengi Posted August 27, 2013 Share Posted August 27, 2013 Hi everybody, I am just learning to use the Confirm Box feature in Javascript. I have tried to add it onto HTML form's submit buttons and checkboxes without problem. But when I add it to a HTML drop down list the behavior is a bit different and unexpected. My code looks like this: <select name='mychoice' onclick='return confirm("Are you sure?");'> <option value='1' selected='selected'>Apple</option> <option value='2'>Orange</option> <option value='3'>Lemon</option> </select> When I select one choice (whether it is Apple or Orange or Lemon), the Confirm Box pops up as expected. The problem is, when I press Cancel, the original choice will not appear, unlike checkboxes. In other words, pressing OK and Cancel will lead to the same result. Is my code wrong, or is there any simple way to make the original choice re-appear when Cancel is pressed? Thanks, pengi. Link to comment https://forums.phpfreaks.com/topic/281602-using-javascript-confirm-box-on-html-drop-down-list/ Share on other sites More sharing options...
MDCode Posted August 27, 2013 Share Posted August 27, 2013 You need to validate the response if(confirm("Are you sure?")) { //Pressed ok } else { //Pressed cancel } Link to comment https://forums.phpfreaks.com/topic/281602-using-javascript-confirm-box-on-html-drop-down-list/#findComment-1447007 Share on other sites More sharing options...
pengi Posted August 27, 2013 Author Share Posted August 27, 2013 Thank you very much. But inside the if-else, what should I do so that the original selection can be shown? I have searched the Internet and seems a solution is __doPostBack(), but the following code still does not work: <select id="z" name="sy" onChange="if (confirm('Are you sure?')) {alert('1');} else {__doPostBack('z','');}"><option value='1' selected='selected'>Apple</option><option value='2'>Orange</option><option value='3'>Lemon</option></select> Am I doing something wrong in the else part? Thanks, pengi. Link to comment https://forums.phpfreaks.com/topic/281602-using-javascript-confirm-box-on-html-drop-down-list/#findComment-1447038 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.