gpt4 book ai didi

javascript - 如何以克隆形式(javascript)重命名所有 id?

转载 作者:行者123 更新时间:2023-11-29 19:12:26 26 4
gpt4 key购买 nike

我有一个以这种方式克隆的表单 (commonStuff)(以及一个 html 页面,在 jquery mobile 中设计,表单多次出现,克隆):

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>

将重命名应用于外部 div,例如“fieldcontain”和“controlgroup”。如果我删除这两个 div,它会工作,但图形效果是 Not Acceptable ...

到目前为止,我得到的问题是在最后一个“else if” block 中,因为它不关心 sibling ,但我真的不知道如何解决这个问题。有什么想法吗?

编辑:此代码来自此答案 How to create a tabbed html form with a common div

最佳答案

当您使用 jQuery Mobile 时,jQuery 将可用。我希望这段代码能为您指明正确的方向:

var i = 0;
$("form").each(function() {
$(this).find("input, select").each(function() {
$(this).attr("id", "form" + i + "_" + $(this).attr("id"));
$(this).attr("name", "form" + i + "_" + $(this).attr("name"));
});
$(this).find("label").each(function() {
$(this).attr("for", "form" + i + "_" + $(this).attr("for"));
});
i++;
});

编辑:

我了解您当前的方法因标签而失败。考虑将您的输入元素包装在 label 标记内。在这种情况下,您将不需要 for 属性。这符合文档:http://api.jquerymobile.com/checkboxradio/考虑一下:

HTML

<form>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<legend>Address:</legend>
<label><input type="radio" name="radio-address" id="radio-address_female" value="1" checked="checked" />Address 1</label>
<label><input type="radio" name="radio-address" id="radio-address_male" value="2" />Address 2</label>
</fieldset>
</div>
<div data-role="fieldcontain" class="ui-field-contain ui-body ui-br">
<label><input type="text" name="age" id="age" value="" class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset">Age:</label>
</div>
</form>

jQuery

var i = 0, currentPrefix = "form";
$("form").each(function() {
$(this).find("input, select").each(function() {
$(this).attr("id", currentPrefix + i + "_" + $(this).attr("id"));
$(this).attr("name", currentPrefix + i + "_" + $(this).attr("name"));
});
i++;
});

工作 fiddle :https://jsfiddle.net/EliteSystemer/8sx6rnwo/

关于javascript - 如何以克隆形式(javascript)重命名所有 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37789037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com