2011-01-07 5 views
0

モーダルフォームに2つのラジオグループがあります。私は、json文字列から各グループの値を設定する方法がわかりません.Json文字列の最後の2つの値は"役割"と "活性化"。 Activateは、Yesの場合は1、Nowの場合は0です。管理者、スタッフ、またはユーザーの役割は1,2または3です。私はjsonの文字列をループし、フィールド名に値を代入します。なぜなら、それらはhtmlですべて同じですからです。jsonからラジオグループを設定する

私のHTMLは次のとおりです。

<div class="form-field bt-space10"> 
    <div class="clear"> 
    <h4 class="fl-space2 ln-22 size-120" style="color:#777777; font-size: 12px; font-weight: normal;">Authorization Role: </h4> 

    <div class="form-radio-item clear fl-space2"> 
     <input type="radio" rel="radiogroup" name="role" id="Admin" value="1" class="radio fl-space" /> <label for="Admin" class="fl">Admin</label> 
    </div> 

    <div class="form-radio-item clear fl-space2"> 
     <input type="radio" rel="radiogroup" name="role" id="Staff" value="2" class="radio fl-space" /> <label for="Staff" class="fl">Staff</label> 
    </div> 

    <div class="form-radio-item clear fl-space2"> 
     <input type="radio" rel="radiogroup" name="role" id="User" value="3" class="radio fl-space" /> <label for="User" class="fl">User</label> 
    </div> 
    </div> 
    <div class="clear"> 
    <h4 class="fl-space2 ln-22 size-120" style="color:#777777; font-size: 12px; font-weight: normal;">Account Activated: </h4> 

    <div class="form-radio-item clear fl-space2"> 
     <input type="radio" rel="radiogroup1" name="activated" id="Yes" class="radio fl-space" /> <label for="radio4" class="fl">Yes</label> 
    </div> 

    <div class="form-radio-item clear fl-space2"> 
     <input type="radio" rel="radiogroup1" name="activated" id="No" class="radio fl-space" /> <label for="radio5" class="fl">No</label> 
    </div> 
    </div> 

</div><!-- /.form-field--> 

私の着信JSON文字列(必要であれば、私は「役割」への復帰を変えることができる:「管理者」、等。そして、「活性化」:「はい」、など。):

{"op": "UPDATE", "id": "7","email": "[email protected]","name": "Joe Public","address": "123 Any Street","city": "AnyTown","zipcode": "12345","state": "PA","contact_name": "","phone": "8885551212","fax": "","company_branch": "","company_name": "","activated": "1","role": "1" } 

私は、フォーム上の他のフィールドを移入するために使用するコード:

function editUser(rowID, callback) { 
    var ret; 
    jQuery.fancybox({ 
     modal : true, 
     padding : 0, 
     cache : false, 
     overlayOpacity : 0.5, 
     href : "userEdit.html", 
     onComplete : function() { 
      InitProfileMenu() 
      var tsTimeStamp= new Date().getTime(); 
      $.getJSON("includes/GetUserDetails.php?tsTimeStamp&id=" + rowID,  
       function(data) { 
        $.each(data, function(i, item) { 
        $('#' + i).val(item); 
       }); 
      }) 
      jQuery("#editUser_cancel").click(function() { 
       ret = false; 
       jQuery.fancybox.close(); 
      }) 
      jQuery("#editUser_ok").click(function() { 
       jQuery.fancybox.close(); 
       ret = true; 
      }) 
     }, 

私は死にこれをGoogleで検索して見つけるなどしていませんolution。誰にでもアイデアはありますか?または彼らが私にこれを理解するのに役立つと指摘できるリソース?

ありがとうございます! Dennis

答えて

1

これは、html属性が互いに似ていないため、JSON文字列内のすべての値に対して一般的な方法で解決することは困難です。 JSONデータの各要素について

  • を「活性化」という名前のラジオ入力にvalue属性を追加

    1. :私は2つの変更を提案ラジオは、名前と値の属性セレクタを使用している場合、フォーム入力の種類を確認します正しいものを確認してください。

      var myJsonData = { 
          "op": "UPDATE", 
          "id": "7", 
          "email": "[email protected]", 
          "name": "Dennis Megarry", 
          "address": "123 Any Street", 
          "city": "AnyTown", 
          "zipcode": "12345", 
          "state": "PA", 
          "contact_name": "", 
          "phone": "8885551212", 
          "fax": "", 
          "company_branch": "", 
          "company_name": "", 
          "activated": "1", 
          "role": "1" 
      }; 
      
      function updateFormWithJson(dataObj) { 
          $.each(dataObj,function(i,item){ 
           if($('input[name="' + i + '"]:first').attr("type") == "radio"){ 
            $('input[name="' + i + '"][value="' + item + '"]').attr("checked","checked"); 
           } 
          }); 
      }; 
      
      $(document).ready(function() { 
          updateFormWithJson(myJsonData); 
      }); 
      
      <div class="form-field bt-space10"> 
          <div class="clear"> 
          <h4 class="fl-space2 ln-22 size-120" style="color:#777777; font-size: 12px; font-weight: normal;">Authorization Role: </h4> 
      
          <div class="form-radio-item clear fl-space2"> 
           <input type="radio" rel="radiogroup" name="role" id="Admin" value="1" class="radio fl-space" /> <label for="Admin" class="fl">Admin</label> 
          </div> 
      
          <div class="form-radio-item clear fl-space2"> 
           <input type="radio" rel="radiogroup" name="role" id="Staff" value="2" class="radio fl-space" /> <label for="Staff" class="fl">Staff</label> 
          </div> 
      
          <div class="form-radio-item clear fl-space2"> 
           <input type="radio" rel="radiogroup" name="role" id="User" value="3" class="radio fl-space" /> <label for="User" class="fl">User</label> 
          </div> 
          </div> 
          <div class="clear"> 
          <h4 class="fl-space2 ln-22 size-120" style="color:#777777; font-size: 12px; font-weight: normal;">Account Activated: </h4> 
      
          <div class="form-radio-item clear fl-space2"> 
           <input type="radio" rel="radiogroup1" name="activated" id="Yes" value="1" class="radio fl-space" /> <label for="radio4" class="fl">Yes</label> 
          </div> 
      
          <div class="form-radio-item clear fl-space2"> 
           <input type="radio" rel="radiogroup1" name="activated" id="No" value="0" class="radio fl-space" /> <label for="radio5" class="fl">No</label> 
          </div> 
      </div> 
      
  • +0

    これは完璧に機能しました。私は同じ機能でそれをやろうとしていましたが、私が間違った方向にいたように見えます..もう一度..笑ありがとう! – Dennis

    関連する問題