in reply to Re: JSON Return Values
in thread JSON Return Values

I would appreciate some insight here:

I am used to drop downs being bound to database fields

I have a select drop down on a form used to populate the insert/update query sent to the server.

<td><select required id="position" name="position" onChange="changeFla +g(1)" > <option value="0">Select <option value="1">Board Member <option value="2">Administration <option value="3">General Member </select></td>

When I repopulate the form field with JSON

$('form').loadJSON(data);

, I expect the data from the database field to be displayed. All the other form input fields are populated correctly.

By the way, this: onChange="changeFlag(1) is not calling anything. A dummy

In reading:

Populating form element Form is populated with JSON object using the following JavaScript call: $('form').loadJSON(data); This call matches object properties with the form elements by name and depending on the type of the form element populates value. Following rules are used while the form elements are populated:

Multiple selection lists

When plugin matches property with multiple selection list element, it expect that property is array of values that should be selected in the list. Plugin selects all values in the multi selection list that exist in the property of the JavaScript object.

Assuming that a "select" control comes under the definition of "Multiple selection lists", it sounds as if it would work and display the applicable JSON content.

I set up a test with a select where I use a button to change the "value" of the select index and that works fine.

Evidently, $('form').loadJSON(data), does not address the indexing as implied above.

Replies are listed 'Best First'.
Re^3: JSON Return Values
by Corion (Patriarch) on Mar 22, 2018 at 15:07 UTC

    Where in your question does Perl come into play?

    I think your question is mostly about jQuery, which is off-topic here. You should first check that sending the appropriate (hard-coded) JSON works. Then you can debug your Perl code until it creates the same JSON.

    If you don't know what the correct JSON to create is, no fudging on the Perl side of the world will help you. You will need jQuery expertise, which certainly can be found. But this is not the forum for jQuery expertise.

      You are so correct. Forgot where I was. Perl on one side HTML on the other, JQuery in the middle.

Re^3: JSON Return Values
by tultalk (Monk) on Mar 22, 2018 at 15:42 UTC

    The data back from JSON includes "position":"3" so the information is there.

    The javascript looks good:

    (function ($) { $.fn.loadJSON = function (obj, options) { function setElementValue(element, value, name) { var type = element.type || element.tagName; if (type == null) return; type = type.toLowerCase(); switch (type) { case 'radio': if (value.toString().toLowerCase() == element.valu +e.toLowerCase()) $(element).attr("checked", "checked"); break; case 'checkbox': if (value) $(element).attr("checked", "checked"); break; case 'select-multiple': var values = value.constructor == Array ? value : +[value]; for (var i = 0; i < element.options.length; i++) { for (var j = 0; j < values.length; j++) { element.options[i].selected |= element.opt +ions[i].value == values[j]; } } break; case 'select': case 'select-one': case 'text': case 'hidden': $(element).attr("value", value); break;

    At a loss. I don't see "value as either a global attribute or an attribute of select.

    Attributes defines: value <button>, <input>,

  • , <option>, <meter>, <progress>, <param>

    So it is an attribute of option not select

      Resolved by using a more recent script as suggested on jquery forum.

      Thanks>