我已经尝试了很多来添加换行符,但它不起作用...用行括号替换\ n 我将在记事本中以txt格式保存html表单,查看所有输入文本,其中一行是我不想要的...
请你帮忙。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="base64.js"></script>
<script type="text/javascript">
<!--
function buildData(){
var txtData = "00"+$("#nameField").val()+
" "+$("#title").val()+
"0000000000"+$("#lastNameField").val()+
"\r\n Gender: "+($("#genderMale").is(":checked")?"Male":"Female");
return txtData;
}
// This will be executed when the document is ready
$(function(){
// This will act when the submit BUTTON is clicked
$("#formToSave").submit(function(event){
event.preventDefault();
var txtData = buildData();
window.location.href="data:application/octet-stream;base64,"+Base64.encode(txtData);
});
// This will act when the submit LINK is clicked
$("#submitLink").click(function(event){
var txtData = buildData();
$(this).attr('download','sugguestedName.txt')
.attr('href',"data:application/octet-stream;base64,"+Base64.encode(txtData));
});
});
//-->
</script>
</head>
<body>
<form method="post" action="" id="formToSave">
<dl>
<dt>Date:</dt>
<dd>
<input type="text" id="nameField" value="00002014" />
<input type="text" id="title" value="Credit Card Payment" />
</dd>
<dt>Card No:</dt>
<dd><input type="text" id="lastNameField" value="Last Name" /></dd>
<dt>Gender:</dt>
<dd><input type="radio" checked="checked" name="gender" value="M" id="genderMale" />
Male
<input type="radio" checked="checked" name="gender" value="F" />
Female
</dl>
<p><a href="javascript://Save as TXT" id="submitLink">Save as TXT</a></p>
<p><button type="submit"><img src="http://www.suttonrunners.org/images/save_icon.gif" alt=""/> Save as TXT</button></p>
提前谢谢......
答案 0 :(得分:0)
我认为你的问题不是在Javascript中,而是以后的方式显示。试试alert(buildData())
,它是否正常运行?如果您要将文字附加到DOM,则必须使用HTML换行符,例如<br>
。
答案 1 :(得分:0)
尝试删除添加的jQuery后运行上面提到的。它确实按预期工作。