JQuery-使用嵌套的each()更新XML字符串。原始字符串未更新
新用户和第一个问题。。 我使用嵌套的each()解析XML字符串。我找到要查找的节点并更改属性的值,但是原始xml字符串保持不变 下面是XML和JQuery。在代码中查找警报以查看更新发生的位置。不管怎样,在所有的each都播放完原始的xml字符串之后,它还是原来的样子吗?我做错了什么 谢谢 XML:JQuery-使用嵌套的each()更新XML字符串。原始字符串未更新,jquery,xml-parsing,Jquery,Xml Parsing,新用户和第一个问题。。 我使用嵌套的each()解析XML字符串。我找到要查找的节点并更改属性的值,但是原始xml字符串保持不变 下面是XML和JQuery。在代码中查找警报以查看更新发生的位置。不管怎样,在所有的each都播放完原始的xml字符串之后,它还是原来的样子吗?我做错了什么 谢谢 XML: <Test TestID="712" UserID="1231230192831039812"> <Question id="713"> <AnswerS
<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>
您可以执行上述所有操作并通过引用访问原始DOM元素,也可以使用.html(XML)或其他内容替换或附加当前内容上的XML内容。当您调用
$(XML)
时,您正在创建一个新的jQuery对象,其中包含XML
字符串中定义的节点;这两个人现在完全没有联系了。如果您想获得一个新的XML字符串来表示已更改的状态,则需要从该对象获取该字符串。谢谢。所以我必须再次连接所有节点并重新创建xml以获得更新的值?是否有一种简单的方法可以连接所有节点,或者我必须使用更新的属性值创建原始xml格式?我将存储对通过调用$(xml)
创建的jQuery对象的引用,因为这将反映以后的更改(假设您使用该变量代替当前代码中的任何$(xml)
调用)。这里有几个关于从jQuery对象获取XML字符串的问题,所以请搜索其中一个。如您所说,如何存储对通过调用$(XML)创建的jQuery对象的引用?然后,我将了解如何从该对象获取XML字符串代码>。然后xmlObject
将引用通过调用$(xml)
返回的jQuery对象。
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);
}