xml解析jquery - 如何从具有不同属性的重复XML标记中提取单个值

时间:2020-12-30 15:40:05

So i'm trying to just get a value extracted from my XML, the whole code (well almost but the general idea) is on jsfiddle here http://jsfiddle.net/TQzkj/4/

所以我试图从我的XML中获取一个值,整个代码(几乎是一般的想法)在jsfiddle这里http://jsfiddle.net/TQzkj/4/

All i'm trying to do is to extract the value of Option Value=1 from the XML which is Tops

我所要做的就是从Tops中提取Option Value = 1的值

        xml = '<Set>    <Question>        
        <QuestionId>278</QuestionId>

        <Option Value='1'>Tops</Option>
        <Option Value='2'>Skirts</Option>
        <Option Value='3'>Shoes</Option>        </Question>    <Points>0</Points>      
        <BarHide>8</BarHide></Set>';

i'm using the following code

我正在使用以下代码

[Code]

$(xml).find( "Question" ).each(
function(){
    var item = $(this), 
    id =  item.find('QuestionId').text();

    if(id==="278"){
        var option = item.find('Option');
        var itemid = option.attr('Value');

        if(itemid === "1"){
            $("#button1").attr("value", option[0]); //this is the line that is wrong and needs fixing
        }
    }
    //itemid =  item.attr('id');
}

);

[/Code]

When I debug the value option I see 3 elements and I see the text that I need in textContext but I don't know how to extract them.

当我调试值选项时,我看到3个元素,我在textContext中看到了我需要的文本,但我不知道如何提取它们。

1 个解决方案

#1


0  

It's not an array, jQuery's find() returns a set of elements in an object, so just do:

它不是一个数组,jQuery的find()返回一个对象中的一组元素,所以这样做:

$("#button1").val(option.text());

FIDDLE

#1


0  

It's not an array, jQuery's find() returns a set of elements in an object, so just do:

它不是一个数组,jQuery的find()返回一个对象中的一组元素,所以这样做:

$("#button1").val(option.text());

FIDDLE