用JavaScript复制条目和XML
Replicating entries and XML with JavaScript
我正在构建一个用户提交系统,其中用户可以输入数据,XML将被导出(可以由不同的软件读取),但是我遇到了一个问题,当我复制用户输入信息的表单时,XML只从第一个获取信息。
有什么建议吗?
个人测试代码:
HTML:$(function () {
$('#SubmitButton').click(update);
});
var added = [
''t't
<bam_file desc='"<?channeldescription?>'" record_number='"<?channelrecordnumber?>'" hex_color='"<?channelhexcolor?>'" bam_link='"<?channelbamlink?>'">',
''t't</bam_file>
'
].join(''r'n');
var adding = [
''t't
<bam_file desc='"<?channeldescription?>'" record_number='"<?channelrecordnumber?>'" hex_color='"<?channelhexcolor?>'" bam_link='"<?channelbamlink?>'">',
''t't</bam_file>
'
].join(''r'n');
function update() {
var variables = {
'channeldescription': $('#channeldescription').val(),
'channelrecordnumber': $('#channelrecordnumber').val(),
'channelhexcolor': $('#channelhexcolor').val(),
'channelbamlink': $('#channelbamlink').val()
};
var newXml = added.replace(/<'?('w+)'?>/g,
function(match, name) {
return variables[name];
});
var finalXML = newXml;
$('#ResultXml').val(finalXML);
$('#DownloadLink')
.attr('href', 'data:text/xml;base64,' + btoa(finalXML))
.attr('download', 'bamdata.xml');
$('#generated').show();
}
$(function () {
$("#CloneForm").click(CloneSection);
});
function CloneSection() {
added = added + ''n' + adding;
$("body").append($("#Entries:first").clone(true));
}
<!DOCTYPE html>
<html>
<head>
<script src="cdnjs.cloudflare.com/ajax/libs/processing.js/1.4.1/processing-api.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<body>
<div id="Entries" name="Entries">
<legend class="leftmargin"> Entry </legend>
<form class="form">
<fieldset>
<div class="forminput">
<label for="channel-description" class="formtextarea">Description</label>
<textarea id="channeldescription" name="channeldescription" type="text"></textarea>
</div>
<div class="forminput">
<label for="channel-record_number">Record number</label>
<input id="channelrecordnumber" name="channelrecordnumber"/>
</div>
<div class="forminput">
<label for="channel-hex_color">Hex color</label>
<input id="channelhexcolor" name="channelhexcolor"/>
</div>
<div class="forminput">
<label for="channel-bam_link">RNA-Seq Data/BAM file Repsitory Link</label>
<input id="channelbamlink" name="channelbamlink" type="text" data-help-text="bam_link"/>
</div>
</fieldset>
</form>
</div>
</body>
<div id="Cloning" class="button_fixed">
<p>
<button id="CloneForm">Add another entry</button>
<button id="SubmitButton">Generate XM</button>
</p>
</div>
<div id="generated" style="display:none">
<h2>bamdata.xml</h2>
<a href="#" id="DownloadLink">Download XML</a>
<textarea id="ResultXml" style="width: 100%; height: 30em" readonly></textarea>
</div>
</div>
</html>
http://www.w3schools.com/code/tryit.asp?filename=F0TWR6VRQZ3J 将id更改为类,使用循环获取所有条目
<!DOCTYPE html>
<html>
<head>
<script src="cdnjs.cloudflare.com/ajax/libs/processing.js/1.4.1/processing-api.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
.leftmargin {
margin-left: 2%;
}
.form {
background-color: #CBE8BA;
border-radius: 25px;
margin-left: 2%;
margin-right: 2%;
padding-top: 1%;
padding-bottom: 1%;
}
.forminput {
padding-left: 1.5%;
padding-top: 0.5%;
display: flex;
}
.button_fixed {
position: fixed;
margin-left: 2%;
bottom: 1%;
}
</script>
<script>
$(function () {
$('#SubmitButton').click(function(){
var finalXML = '';
$(".Entries").each(function(i,v) {finalXML +=update(finalXML,v)
$('#ResultXml').val(finalXML);
$('#DownloadLink')
.attr('href', 'data:text/xml;base64,' + btoa(finalXML))
.attr('download', 'bamdata.xml');
$('#generated').show();
});
});
});
var added = [
''t't<bam_file desc='"<?channeldescription?>'" record_number='"<?channelrecordnumber?>'" hex_color='"<?channelhexcolor?>'" bam_link='"<?channelbamlink?>'">',
''t't</bam_file>'
].join(''r'n');
var adding = [
''t't<bam_file desc='"<?channeldescription?>'" record_number='"<?channelrecordnumber?>'" hex_color='"<?channelhexcolor?>'" bam_link='"<?channelbamlink?>'">',
''t't</bam_file>'
].join(''r'n');
function update(finalXML,v) {
var variables = {
'channeldescription': $(v).find('.channeldescription').val(),
'channelrecordnumber': $(v).find('.channelrecordnumber').val(),
'channelhexcolor': $(v).find('.channelhexcolor').val(),
'channelbamlink': $(v).find('.channelbamlink').val()
};
var newXml = added.replace(/<'?('w+)'?>/g,
function(match, name) {
return variables[name];
});
return newXml;
}
$(function () {
$("#CloneForm").click(CloneSection);
});
function CloneSection() {
$("body").append($(".Entries:first").clone(true));
}
</script>
<body>
<div class="Entries" name="Entries">
<legend class="leftmargin"> Entry </legend>
<form class="form">
<fieldset>
<div class="forminput">
<label for="channel-description" class="formtextarea">Description</label>
<textarea class="channeldescription" name="channeldescription" type="text"></textarea>
</div>
<div class="forminput">
<label for="channel-record_number">Record number</label>
<input class="channelrecordnumber" name="channelrecordnumber"/>
</div>
<div class="forminput">
<label for="channel-hex_color">Hex color</label>
<input class="channelhexcolor" name="channelhexcolor"/>
</div>
<div class="forminput">
<label for="channel-bam_link">BAM file Repsitory Link</label>
<input class="channelbamlink" name="channelbamlink" type="text" data-help-text="bam_link"/>
</div>
</fieldset>
</form>
</div>
</body>
<div id="Cloning" class="button_fixed">
<p>
<button id="CloneForm">Add another entry</button>
<button id="SubmitButton">Generate XM</button>
</p>
</div>
<div id="generated" style="display:none">
<h2>bamdata.xml</h2>
<a href="#" id="DownloadLink">Download XML</a>
<textarea id="ResultXml" style="width: 100%; height: 30em" readonly="readonly"></textarea>
</div>
</div>
</html>
演示:http://www.w3schools.com/code/tryit.asp?filename=F0TXIUR1CYE4
相关文章:
- 我应该如何从xml文件构建一个javascript页面
- 将XML转换为普通的旧JavaScript对象
- 尝试读取空节点时出现 XML JavaScript 错误
- XML/JavaScript 解析节点 - 按名称指定子节点
- $(this).getAttribute 在使用 $.each XML Javascript 时不是一个函数
- 以制表符分隔的文本文件转换为XML(Javascript或PHP)
- 解析简单的XML-Javascript/Ajax
- 将子单元格放入单元格dymo XML Javascript API
- XML javascript绘制网络图
- Adobe Air应用程序全屏- HTML/XML/Javascript
- XML Javascript绘制甜甜圈图
- 无法读取属性'documentElement'null"在PHP/XML/JavaScript
- Safari问题,变量未定义(XML/Javascript)
- 通过PHP转换XML &Javascript函数
- 使用R抓取xml/javascript表
- n -三元组到RDF/XML JavaScript的NodeJS转换器
- 返回readystatechange接收到的XML - JavaScript
- xml javascript获取html属性值
- XML, Javascript -未捕获类型错误:不能调用方法'getElementsByTagName'
- 如何从XML/javascript中获取元素