Reputation: 3189
What I am trying to do is to populate my input fields based on JSON object. Below are the fields with their default values:
<input type="hidden" name="id"/>
<input id="enabled1" name="enabled" type="checkbox" value="true"> // not sure why value="true" here, by default it should be false
The JSON object has the following format:
[{enabled:false, id:184}]
Based on the JSON object, I will iterate its properties and assign the value to the field accordingly. The iterate function is as follows:
for ( var propertyName in aData) {
if (aData.hasOwnProperty(propertyName)){
// check if field exist
var elem = $("input[name="+propertyName+"]");
var exist = elem.size() == 1;
// if exist, update field with selected info
if (exist){
elem.val(aData[propertyName]);
}
}
}
After the iterate function runs the fields are shown like below:
<input type="hidden" name="id" value="184">
<input id="enabled1" name="enabled" type="checkbox" value="false">
Now if I tick the checkBox and submit the information to server, I want the value to be true but somehow the value is still false. What did I miss out here ?
Upvotes: 0
Views: 2005
Reputation: 7228
But that's very much expected. Since, selecting a checkbox does not have any effect on the value of the html element it changes the checked status from unchecked to checked. When you try to get the value on the server side it would return you with the overridden value.
Upvotes: 2
Reputation: 3324
I'm not sure, but I think input
tag should have close tag (slash /
) in itself:
<input type="hidden" name="id" value="184"/>
<input id="enabled1" name="enabled" type="checkbox" value="false"/>
Please check. -Han-
Upvotes: 0