gpt4 book ai didi

How to add a bullet point in a form in Javascript?(如何在Java脚本中的表单中添加项目符号?)

转载 作者:bug小助手 更新时间:2023-10-26 20:28:08 26 4
gpt4 key购买 nike



I have a javascript code inside a form, This javascript is supposed to put a bullet point to the previous line, every time that I press the 'enter' key. However, when I do press the enter key it does put the bullet point, but when I press the enter key again, an additional bullet appears. For instance, I have line 1, line 2, and line 3. When I type line one and press enter, a bullet appears behind line one, but when I type line 2 and press enter, line one suddenly has 2 bullets behind it and line 2 has a bullet added to it.

我在一个表单中有一段javascrip代码,每次我按回车键时,这个javascrip都会在前一行加上一个项目符号。然而,当我按Enter键时,它确实会放入项目符号,但当我再次按Enter键时,会出现一个额外的项目符号。例如,我有第一行、第二行和第三行。当我键入第一行并按Enter时,第一行后面会出现一个项目符号,但当我键入第二行并按Enter时,第一行后面突然有两个项目符号,而第二行则增加了一个项目符号。




This is the code:

这就是代码:


// Function to format the "roles" textarea into bullet points
document.addEventListener('keydown', function (e) {
if (e.target.id === 'id_form-0-roles' && e.keyCode === 13) {
formatRoles(e.target);
e.preventDefault();

}
});

function formatRoles(textarea) {
let lines = textarea.value.split('\n');
let formattedText = '';
lines.forEach(function (line) {
if (line.trim() !== '') {
formattedText += '• ' + line.trim() + '\n';
} else {
formattedText += '\n'; // Add a new line for empty lines
}
});
textarea.value = formattedText;
}

</script>

I expect the javascript to put just one bullet point in the preceding line after the enter key is pressed, such that each line has a bullet point behind it. This is what I want:

我希望在按下Enter键之后,该脚本只在前一行中放入一个项目符号,这样每一行后面都有一个项目符号。这就是我想要的:



  • line 1

    1号线



  • line 2

    2号线



  • line 3

    3号线




更多回答
优秀答案推荐

When formatRoles sets the value for lines, its splits the entire existing textarea text by the new lines. The bullet points are characters in the text area, so after typing the first line the result for array lines is ["• hello","world"] and then you append a bullet to each resulting in ["• • hello","• world","another new line"] and so on.

当FormatRoles设置行值时,它将用新行拆分整个现有文本区文本。项目符号是文本区域中的字符,因此在键入第一行之后,数组行的结果是[“·Hello”,“world”],然后在每个项目符号后面加上一个项目符号,结果是[“··Hello”,“·world”,“Another new line”],依此类推。


A solution for this would be looking at only the last line when adding the bullet.

解决此问题的方法是在添加项目符号时只查看最后一行。




// Function to format the "roles" textarea into bullet points
document.addEventListener("keydown", function (e) {
if (e.target.id === "id_form-0-roles" && e.keyCode === 13) {
formatRoles(e.target);
e.preventDefault();
}
});

function formatRoles(textarea) {
let lines = textarea.value.split("\n");
let formattedText = "";
lines.forEach((line, i) => {
if (i === lines.length - 1 && line.trim() !== "") { //only apply the bullet if its the last line and not empty.
formattedText += "• " + line.trim() + "\n";
} else {
formattedText += line + "\n";
}
});
textarea.value = formattedText;
}

<textarea id="id_form-0-roles" style="width: 400px;height:100px;"></textarea>  




更多回答

Hello @cub, thank you for your contribution. upon implementing those changes, the code is still not working as intended, so I see that now when I click enter a bullet point is displayed but when I type another sentence and press enter, the first line is substituted by the second line.

您好@幼崽,感谢您的贡献。在实现这些更改后,代码仍然不能按预期工作,所以现在当我按Enter时,会显示一个项目符号,但当我键入另一句话并按Enter时,第一行将被第二行替换。

whoops fixed. same idea still, only add the bullet on the last line.

哎呀,修好了。想法还是一样的,只是在最后一行加上了项目符号。

wow! thank you so much, I truly appreciate your help. IT WORKED!!

哇!太感谢你了,我真的很感谢你的帮助。成功了!!

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