My first try with UI - Am I doing it right?
My first attempt using the jQuery UI, and it actually works! A live example is located
here. It is an auto-complete within a modal. The returned data from the server is a JSON consisting of id=zipcode and value="city, state, zipcode". I would appreciate any critique whether I am doing it correctly. Also, any pointers how I can create a warning if the inputted text results in no returned values.
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta charset="UTF-8" />
- <title>Jquery UI</title>
- <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" type="text/css" media="all" />
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js" type="text/javascript"></script>
- <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.js" type="text/javascript"></script>
- <style type="text/css">
- body { font-size: 62.5%; }
- input.text { margin-bottom:12px; width:95%; padding: .4em; }
- .ui-dialog .ui-state-error { padding: .3em; }
- .validateTips { border: 1px solid transparent; padding: 0.3em; }
- </style>
- <script type="text/javascript">
- $(function() {
-
- var zipcode,
- locations = $("#locations"),
- tips = $("#validateTips");
-
- $('#states').change(function() {
- locations.autocomplete( 'option' , 'source', 'getLocations.php?state='+$(this).val());
- zipcode=0;
- locations.val('');
- });
-
- locations.autocomplete({
- source: "getLocations.php",
- minLength: 2,
- select: function(event, ui) {
- zipcode=ui.item ? ui.item.id : '';
-
- }
- });
- function updateTips(t) {
- tips
- .text(t)
- .addClass('ui-state-highlight');
- setTimeout(function() {
- tips.removeClass('ui-state-highlight', 1500);
- }, 500);
- }
- $("#dialog-form").dialog({
- autoOpen: false,
- width: 350,
- modal: true,
- buttons: {
- 'Cancel': function() {
- $(this).dialog('close');
- },
- 'Delete Location': function() {
- $('#location').val('');
- $('#zipcode').val('');
- $(this).dialog('close');
- },
- 'Save Location': function() {
- if(zipcode)//Don't save if zipcode not selected
- {
- $('#location').val(locations.val());
- $('#zipcode').val(zipcode);
- $(this).dialog('close');
- }
- else
- {
- updateTips("Select a location before saving.");
- }
- }
- }
- });
-
- //Maybe change shadding for Save Location button until a location is selected?
- //var buttons = $( "#dialog-form" ).dialog( "option", "buttons" );
- $('#change_location').button().click(function()
- {
- $('#dialog-form').dialog('open');
- zipcode=0;
- $('#states').val(0);
- $('#locations').val('');
- });
- });
- </script>
- </head>
- <body>
- <div id="dialog-form" title="Select a Location">
- <p id="validateTips">A location must be selected before saving.</p>
- State:<select id="states">
- <option value="0">Select State</option>
- <option value="CA">California</option>
- <option value="WA">Washington</option>
- <option value="NY">New York</option>
- </select>
- <div class="ui-widget">City:<input id="locations" /></div>
- </div>
- <button id="change_location">Add City</button>
- <input type="text" id="location" name="location" />
- <input type="text" id="zipcode" name="zipcode"/>
- </body>
- </html>