我有一个以这种方式克隆的表单(commonStuff)(以及一个在 jquery mobile 中设计的 html 页面,该表单多次出现,被克隆):
var commonClone = commonStuff.cloneNode(true);
和这个功能
function renameNodes(node) {
var i;
if (node.length) {
for (i = 0; i < node.length; i += 1) {
renameNodes(node[i]);
}
} else {
// rename any form-related elements
if (typeof node.form !== 'undefined') {
node.id = currentPrefix + '_' + node.id;
node.name = currentPrefix + '_' + node.name;
// This assumes that form elements do not have child form elements!
} else if (node.children) {
renameNodes(node.children);
}
}
}
它将前缀'form1_','form_2'(currentPrefix)添加到表单中任何元素的id和名称,并应用于commonClone(并递归地应用于他的儿子)。
renameNodes(commonClone);
在文本输入的情况下,它可以完美地工作,例如
<div data-role="fieldcontain" class="ui-field-contain ui-body ui-br">
<label for="foo" class="ui-input-text">Age:</label>
<input type="text" name="age" id="age" value="" class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset">
</div>
但它在单选按钮上失败了
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<legend>Address:</legend>
<input type="radio" name="radio-address" id="radio-address_female" value="1" checked="checked" />
<label for="radio-address_female">Address 1</label>
<input type="radio" name="radio-address" id="radio-address_male" value="2" />
<label for="radio-address_male">Address 2</label>
</fieldset>
</div>
将重命名应用于“fieldcontain”和“controlgroup”等外部 div。如果我删除这两个 div,它会起作用,但图形效果是不可接受的......
到目前为止,我发现问题出在最后一个“else if”块中,因为它不关心兄弟姐妹,但我真的不知道如何解决这个问题。任何想法?
编辑:此代码来自此答案How to create a tabbed html form with a common div