title: Radio buttons checking based on JSON response
body: <p>I am trying to load some values from json string to various inputs / fields. My solution works great for text inputs and textfields but I can't get it to work with radiobuttons and checkboxes.
  Could you please help me with this?</p>
  
  <p>In HTML I have some fields like following:</p>
  
  <pre><code>&lt;textarea  rows="3" name="testfield"&gt;&lt;/textarea&gt;
  &lt;input type="text" name="testinput"&gt;
  &lt;label&gt;&lt;input class="radio" type="radio" name="testrad" value="one" checked&gt;&lt;span&gt;1&lt;/span&gt;&lt;/label&gt;
  &lt;label&gt;&lt;input class="radio" type="radio" name="testrad" value="two" &gt;&lt;span&gt;2&lt;/span&gt;&lt;/label&gt;
  &lt;label&gt;&lt;input class="radio" type="radio" name="testrad2" value="one" checked&gt;&lt;span&gt;1&lt;/span&gt;&lt;/label&gt;
  &lt;label&gt;&lt;input class="radio" type="radio" name="testrad2" value="two" &gt;&lt;span&gt;2&lt;/span&gt;&lt;/label&gt;
  &lt;label&gt;&lt;input class="radio" type="radio" name="testrad2" value="three" &gt;&lt;span&gt;2&lt;/span&gt;&lt;/label&gt;
  </code></pre>
  
  <p>With AJAX call I get json string like:</p>
  
  <p><code>{"testfield":"Value of testfield", "testinput":"Value of testinput","testrad":"two","testrad2":"three"}</code></p>
  
  <p>And here is the JS code:</p>
  
  <pre><code>$.ajax({
      type: "POST",
      url: "test.php",
      success: function (echo) {
          var data = JSON.parse(echo);
          for (key in data) {
              var field = $("[name=" + key);                           
              if (field.attr("type") == "radio" || field.attr("type") == "checkbox") {                                
                  if ( field.val() == data[key]) {                                   
                      field.prop("checked", true) 
                  }
              }else {
                  $("[name=" + key).val(data[key]);
              }
          }
      }
  });
  </code></pre>