gpt4 book ai didi

javascript - Jquery用换行符附加文本数据

转载 作者:行者123 更新时间:2023-12-05 00:28:37 27 4
gpt4 key购买 nike

所以我有这个脚本,它从表单中获取数据并将其附加到 HTML div 中。

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<head>
<style>
* {
box-sizing: border-box;
}

div {
padding: 10px;
background-color: #f6f6f6;
overflow: hidden;
}

input[type=text],
textarea,
select {
font: 17px Calibri;
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
}

input[type=button] {
font: 17px Calibri;
width: auto;
float: right;
cursor: pointer;
padding: 7px;
}
</style>
</head>

<body>
<div>


<div>
<input type="text" id="txtName" placeholder="Enter your name" />
</div>
<div>
<input type="text" id="txtAge" placeholder="Enter your age" />
</div>
<div>
<input type="text" id="txtEmail" placeholder="Enter your email address" />
</div>
<div>
<select id="selCountry">
<option selected value="">-- Choose the country --</option>
<option value="India">India</option>
<option value="Japan">Japan</option>
<option value="USA">USA</option>
</select>
</div>
<div>
<textarea id="msg" name="msg" placeholder="Write some message ..." style="height:100px"></textarea>
</div>
<div>
<input type="button" id="bt" value="Write" onclick="writeFile()" />
</div>

</div>
<div class="output-area">
<h4>Output</h4>
<div id="output" class="inner">
</div>
</div>
<span></span>

</body>
<script>
let writeFile = () => {

const name = document.getElementById('txtName');
const age = document.getElementById('txtAge');
const email = document.getElementById('txtEmail');
const country = document.getElementById('selCountry');
const msg = document.getElementById('msg');

let data =
'Name: ' + name.value + ' \r\n ' +
'Age: ' + age.value + ' \r\n ' +
'Email: ' + email.value + ' \r\n ' +
'Country: ' + country.value + ' \r\n ' +
'Message: ' + msg.value;


$('#output').append("<br />" + "<br />");
$('#output').append(document.createTextNode(data));

}
</script>

</html>

但问题是当我填写数据并单击 Write 时它在一行中输出所有表单数据。 (姓名:x 年龄:4 电子邮件:y 国家:日本 留言:z)
我希望它是这样的,
Name: x 
Age: 4
Email: y
Country: Japan
Message: z

Name: x
Age: 42
Email: y
Country: Japan
Message: zd ....
所以我尝试使用javascript创建一个新行(\n),但似乎它不会影响它。
这是我尝试过的。
          'Name: ' + name.value + ' \r\n ' + '\n' +   
'Age: ' + age.value + ' \r\n ' + '\n' +
'Email: ' + email.value + ' \r\n ' + '\n' +
'Country: ' + country.value + ' \r\n ' + '\n' +
'Message: ' + msg.value;
那么这里有什么问题。任何帮助将非常感激。谢谢。

最佳答案

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<head>
<style>
* {
box-sizing: border-box;
}

div {
padding: 10px;
background-color: #f6f6f6;
overflow: hidden;
}

input[type=text],
textarea,
select {
font: 17px Calibri;
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
}

input[type=button] {
font: 17px Calibri;
width: auto;
float: right;
cursor: pointer;
padding: 7px;
}
</style>
</head>

<body>
<div>


<div>
<input type="text" id="txtName" placeholder="Enter your name" />
</div>
<div>
<input type="text" id="txtAge" placeholder="Enter your age" />
</div>
<div>
<input type="text" id="txtEmail" placeholder="Enter your email address" />
</div>
<div>
<select id="selCountry">
<option selected value="">-- Choose the country --</option>
<option value="India">India</option>
<option value="Japan">Japan</option>
<option value="USA">USA</option>
</select>
</div>
<div>
<textarea id="msg" name="msg" placeholder="Write some message ..." style="height:100px"></textarea>
</div>
<div>
<input type="button" id="bt" value="Write" onclick="writeFile()" />
</div>

</div>
<div class="output-area">
<h4>Output</h4>
<div id="output" class="inner">
</div>
</div>
<span></span>

</body>
<script>
let writeFile = () => {

const name = document.getElementById('txtName');
const age = document.getElementById('txtAge');
const email = document.getElementById('txtEmail');
const country = document.getElementById('selCountry');
const msg = document.getElementById('msg');

let data = [
`<p>Name: ${name.value}</p>`,
`<p>Age: ${age.value}</p>`,
`<p>Email: ${email.value}</p>`,
`<p>Country: ${country.value}</p>`,
`<p>Message: ${msg.value}</p>`];


$('#output').append("<br />" + "<br />");

data.forEach(line => $('#output').append(line));

}
</script>

</html>

所以这里我先把每一行数据放到一个 <p>标记,然后将其放入列表中,我使用的是更简洁的模板文字。然后插入要调用的数据 .forEach()在列表上,然后将每一行(或列表的项目)添加到输出中。下面是我改变的部分。
let data = [ 
`<p>Name: ${name.value}</p>`,
`<p>Age: ${age.value}</p>`,
`<p>Email: ${email.value}</p>`,
`<p>Country: ${country.value}</p>`,
`<p>Message: ${msg.value}</p>`];


$('#output').append("<br />" + "<br />");
data.forEach(line => $('#output').append(line));

关于javascript - Jquery用换行符附加文本数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70399711/

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