【问题标题】:JQuery - using nested each() to update XML string. Original string not updatedJQuery - 使用嵌套的 each() 来更新 XML 字符串。原始字符串未更新
【发布时间】:2012-12-02 09:00:21
【问题描述】:

新用户和第一个问题.. 我使用嵌套的 each()s 解析 XML 字符串。我找到了我正在寻找的节点并更改了属性的值,但是原始的 xml 字符串保持不变。

XML 和 JQuery 下面。在代码中查找警报以查看更新发生的位置。无论如何,在每个人都播放完原始的 xml 字符串之后是原样吗?我做错了什么?

谢谢

XML:

<Test TestID="712" UserID="1231230192831039812">
  <Question id="713">
    <AnswerSet id="718" type="5">
      <AnswerSetOption id="740"  IsChecked="Foo" />
      <AnswerSetOption id="741"  IsChecked="Foo" />
      <AnswerSetOption id="742"  IsChecked="Foo" />
    </AnswerSet>
  <AnswerSet id="719" type="5">
    <AnswerSetOption id="740"  IsChecked="Foo" />
    <AnswerSetOption id="741"  IsChecked="Foo" />
    <AnswerSetOption id="742"  IsChecked="Foo" />
  </AnswerSet>
  <AnswerSet id="720" type="5">
    <AnswerSetOption id="740"  IsChecked="Foo" />
    <AnswerSetOption id="741"  IsChecked="Foo" />
    <AnswerSetOption id="742"  IsChecked="Foo" />
  </AnswerSet>
 </Question>
</Test>

脚本:

    function TrackResponse(QuestionID, AnswerSetID, AnswerSetOptionID, InputType) {

        var xml;

        xml = $('#_uiCheckingAnswersHidden').val();

        $(xml).find('Question').each(function () {

            var xmlQuestionID = $(this).attr('id');

            $(this).find('AnswerSet').each(function () {

                var xmlAnswerSetID = $(this).attr('id');

                $(this).find('AnswerSetOption').each(function () {

                    var xmlAnswerSetOptionID = $(this).attr('ID');
                    var checkedValue = $(this).attr('IsChecked');


                    if (InputType == 'radio') {

                        if (QuestionID == xmlQuestionID && AnswerSetID == xmlAnswerSetID && AnswerSetOptionID == xmlAnswerSetOptionID) {

                            alert('Found it');

                            var oldValue;

                            oldValue = $(this).attr('IsChecked');

                            alert('Old value: ' + oldValue)

                            $(this).attr('IsChecked', 'Bar');

                            var newValue;

                            newValue  = $(this).attr('IsChecked');

                            alert('New value: ' + newValue)

                        }
                        else {
                            $(this).attr('IsChecked', 'pp');
                        }
                    }

                    //Checkbox -- if not found updated Checked Attribute to ''
                    if (InputType == 'check') {

                        var checked = $(this).attr('IsChecked');

                        if (QuestionID == xmlQuestionID && AnswerSetID == xmlAnswerSetID && AnswerSetOptionID == xmlAnswerSetOptionID) {

                            if (checked == 'true') {
                                $(this).attr('IsChecked', 'dd');
                            }
                            else {
                                $(this).attr('IsChecked', '');
                            }
                        }
                    }
                });
            });
        });

        alert(xml);
    }

【问题讨论】:

  • 当您调用$(xml) 时,您正在创建一个包含xml 字符串中定义的节点的新jQuery 对象;现在两人已经完全没有联系了。如果您想获取一个新的 XML 字符串来表示更改的状态,您需要从该对象中获取它。
  • 谢谢安东尼。所以我将不得不再次加入所有节点并重新创建 xml 以获得更新的值?有没有一种简单的方法可以连接所有节点,还是我必须使用更新的属性值创建原始 xml 格式?
  • 我会存储对通过调用 $(xml) 创建的 jQuery 对象的引用,因为这将反映以后的更改(假设您使用该变量代替当前代码中的任何 $(xml) 调用)。在 SO 上有几个关于从 jQuery 对象获取 XML 字符串的问题,所以请搜索其中一个。
  • 如你所说,如何存储对通过调用 $(xml) 创建的 jQuery 对象的引用?然后,我将研究如何从对象中获取 XML 字符串。
  • 就这么做:var xmlObject = $(xml);。然后xmlObject 将引用通过调用$(xml) 返回的jQuery 对象。

标签: jquery xml-parsing


【解决方案1】:

您可以执行上述所有操作并通过引用访问原始 DOM 元素,或者您可以使用 .html(xml) 或其他内容替换或附加 XML 内容。

【讨论】:

    猜你喜欢
    • 2013-12-19
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 2018-11-13
    • 2019-09-18
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    相关资源
    最近更新 更多