简体   繁体   English

如何在获取JSON响应时使用javascript中的组合框在选定索引处获取关联的字段值

[英]How to get an associated field value at selected index using combo box in javascript while fetching JSON response

Hello i am developing a phone-gap app and fetching the data from WCF service which returns me JSON response. 您好,我正在开发一个电话缺口应用程序,并从WCF服务中获取数据,该数据将返回JSON响应。 I need to show DesignName in the combo box and pass the associated designId…Any idea how to do that? 我需要在组合框中显示DesignName并传递关联的designId…任何想法该怎么做?

This is the json response.. 这是json响应。

{
    "DesignResult":
    "{\"DesignId\":\"1\",\"DesignName\":\"Bedroom\"}     
     {\"DesignId\":\"2\",\"DesignName\":\"Dining\"}
     {\"DesignId\":\"5\",\"DesignName\":\"Entertainment\"}
     {\"DesignId\":\"4\",\"DesignName\":\"Occasional Tables\"}
     {\"DesignId\":\"6\",\"DesignName\":\"Office\"}
     {\"DesignId\":\"3\",\"DesignName\":\"Upholstery\"}"
}

this is the function with which i can get the text at selected position 这是我可以在选定位置获取文本的功能

var combo1 = document.getElementById("designBox");
var val = combo1.options[combo1.selectedIndex].text;

but this will give me DesignName as i need to show DesignName in the box but i need to get the associated DesignId.. 但这会给我DesignName,因为我需要在框中显示DesignName,但我需要获取关联的DesignId。

With following code i am adding options dynamically 使用以下代码,我可以动态添加选项

for (var i = 0; i < jsonData.Design.length; i++) 
{
opt = document.createElement("option");  
opt.text= jsonData.Design[i].DesignName;
sel = document.getElementById("designBox");  
sel.add(opt); 
}            

modified the code as follows but getting "Null"for DesignId 修改了代码,如下所示,但DesignId为“ Null”

var optElement = document.createElement("option");
optElement.setAttribute('value',jsonData.Design[i].DesignId);
optElement.innerHTML = jsonData.Design[i].DesignName;

to retrieve: 检索:

var combo1 = document.getElementById("designBox");
var test = combo1.getAttribute('value');
alert("test value is...."+test);

Problem is in JSON. 问题出在JSON中。 Its syntactically incorrect. 其语法错误。 I've fixed that for you. 我已经为您修复了该问题。

Read about and study JSON thoroughly here . 在此处全面了解和学习JSON。

var jsonData = '{
    "DesignResult": [
        {
            "DesignId": "1",
            "DesignName": "Bedroom"
        },
        {
            "DesignId": "2",
            "DesignName": "Dining"
        },
        {
            "DesignId": "5",
            "DesignName": "Entertainment"
        },
        {
            "DesignId": "4",
            "DesignName": "OccasionalTables"
        },
        {
            "DesignId": "6",
            "DesignName": "Office"
        },
        {
            "DesignId": "3",
            "DesignName": "Upholstery"
        }
    ]
}';

This is a string returned from server. 这是从服务器返回的字符串。 Now cast it into javascript JSON object using eval() function. 现在,使用eval()函数将其转换为javascript JSON对象。 like, 喜欢,

jsonData = eval('(' + jsonData + ')');

Now you are able to iterate over this JSON object using loops and all. 现在,您可以使用循环和所有方法遍历此JSON对象。 To access the data you need, you have to implement it like, 要访问所需的数据,您必须像这样实现它,

Try this 尝试这个

var len = jsonData.DesignResult.length;

for(var i=0;i<len;i++){
alert("Name - "+jsonData.DesignResult[i].DesignName+" || ID - "+jsonData.DesignResult.DesignId);
}

To fix your code, 要修复您的代码,

var len = jsonData.DesignResult.length;
    for (var i = 0; i < len; i++) 
        {
        opt = document.createElement("option");  
        opt.text= jsonData.DesignResult[i].DesignName;
        opt.value = jsonData.DesignResult[i].DesignId;
        //better way is
        //opt.setAttribute('value',jsonData.DesignResult[i].DesignId);
        sel = document.getElementById("designBox");  
        sel.add(opt); 
        }  

I will recommend doing it with jQuery like, 我将建议使用jQuery之类的方法,

Suppose you want to attach that "ComboBox" to a Div with ID = "myCombo" (update it according to your needs) 假设您要将“ ComboBox”附加到ID =“ myCombo”的Div上(根据需要进行更新)

var selectElement = jQuery("<select />",{id:"mySel"});

for(var i=0;i<len;i++){
var optElement = jQuery("<option />",{value:jsonData.DesignResult[i].DesignId,text:jsonData.DesignResult[i].DesignName);
selectElement.append(optElement);
}

$("#myCombo").append(selectElement);

To access the Name and ID of the selected element, use 要访问所选元素的名称和ID,请使用

var Name = $("#mySel").html();
var ID = $("#mySel").attr('value');

REMOVED OLD CODE 删除旧代码


by comboBox, do you mean SELECT tag in HTML? 通过comboBox,您是指HTML中的SELECT标记吗?

I am finding it hard to understand what it is that you want to do exactly? 我很难理解您到底想做什么?

Generate the drop down menu dynamically? 动态生成下拉菜单? or something else ? 或者是其他东西 ?

Please give us more insight on what you want to do exactly. 请让我们进一步了解您要确切执行的操作。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM