我有两个模式形式的单播组..我很难弄清楚如何从json字符串中设置每个组的值。json字符串中的最后两个值是“角色”和“激活”。Activate为1表示是,或者为0表示当前。管理员、员工或用户的角色为1、2或3。我遍历json字符串并将值分配给字段名,因为它们在html中都是相同的。
我的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字符串(如果需要,我可以将返回值更改为"Role":"Admin“等。和“激活”:“是”等):
{"op": "UPDATE", "id": "7","email": "joe@public.com","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;
})
},我已经用谷歌搜索了这个问题,但没有找到解决方案。有谁有什么想法吗?或者他们可以给我提供的资源来帮助我解决这个问题?
发布于 2011-01-08 00:15:17
对于JSON字符串中的所有值,很难以通用的方式解决这个问题,因为html属性彼此不同。我建议做两个改变:
JSON数据的每个元素添加一个名为"activated"
var myJsonData ={ "op":"UPDATE","id":"7","email":"joe@public.com","name":"Dennis Megarry","address":"123 Any Street","city":"AnyTown","zipcode":"12345","state":"PA","contact_name":"","phone":"8885551212","fax":"","company_branch":"","company_name":"",“激活”:"1","role":"1“};function updateFormWithJson(dataObj) { $.each(dataObj,function(i,item){ if($('inputname="‘+i+ '":first').attr("type") ==“单选”){ $('inputname="’+i+ '"').attr("checked","checked");} });};$(document).ready(function() { updateFormWithJson(myJsonData);});授权角色:已激活管理员员工用户帐户:是否
https://stackoverflow.com/questions/4627286
复制相似问题