2011-01-07 90 views
0

我有一個模式形式的2個廣播組..我有一個困難的時間搞清楚如何設置從json字符串中的每個組的值.. json字符串中的最後2個值是「角色」和「激活」。激活是1表示是或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

對於JSON字符串中的所有值,這是很難以通用方式解決的,因爲html屬性不是彼此不同。我建議兩個更改:

  1. 添加value屬性來命名爲「活化的」
  2. 對於JSON數據的每個元素的無線輸入:檢查表格輸入的類型,如果無線電,使用名稱和值屬性選擇檢查正確的一個。

    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 2011-01-07 17:01:31