在 Asp.Net C# 中使用 javascript 拆分多行文本框值

Asp.Net C# 多行文本框(textbox1)值在地址的 35 个字符后拆分并添加到文本框(textbox2 - 单行)再次拆分接下来的 35 个字符并添加到文本框(textbox3 - 单行)再次拆分接下来的 35 个字符并添加到文本框(textbox4 - Singleline) 再次拆分接下来的 35 个字符并添加到文本框(textbox5 - Singleline)。 注意:在 35 个字符后拆分值时,请确保单词是否不完整,然后在下一行添加该单词,例如(...31 地址 - 这里地址的添加位置是 35 个字符,因此它会被打滑并添加到新的文本框,但我希望新文本框中的单词地址和它们的地址应该算作 35 个字符,依此类推。)

我试过这个但没用..

function CheckReturns() {


            var txt = document.getElementById("TextBox1");

            var splitResults = txt.value.split("\n");


            if (splitResults[splitResults.length - 1].length < 35) {

                if (splitResults[3].length > 0) {

                    document.getElementById('address4').value = splitResults[3];

                }

                return true;

            }

            else {

                document.getElementById('address1').value = splitResults[0];

                document.getElementById('address2').value = splitResults[1];

                document.getElementById('address3').value = splitResults[2]; 

                txt.value = txt.value + "\n";

            }

        }

<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" MaxLength="140" Rows="10" Columns="60"

                onblur="return CheckReturns();" placeholder="Enter Address here.."></asp:TextBox>

            <br />

            Address1:<asp:TextBox ID="address1" runat="server" Width="220px" ></asp:TextBox><br />

            <br />

            Address2:<asp:TextBox ID="address2" runat="server" Width="220px" ></asp:TextBox><br />

            <br />

            Address3:<asp:TextBox ID="address3" runat="server" Width="220px" ></asp:TextBox><br />

            <br />

            Address4:<asp:TextBox ID="address4" runat="server" Width="220px" ></asp:TextBox>


慕森卡
浏览 130回答 1
1回答

明月笑刀无情

这是一个 JS,它实现了我在第一条评论中讨论的逻辑:let longAddr = "this is a really long address with lots of words longer than 35 characters all over the place and i dont know where it is going to stop it just keeps going so it's like the longest address in the world which is a little bit crazy but there you go";let addr = ["","","","",""];for(let i = 0; i < 5 && longAddr.length > 0; i++){&nbsp; &nbsp; if(longAddr.length < 35 || i == 4)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; addr[i] = longAddr;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; let cut = 35;&nbsp; &nbsp; while(longAddr[cut] !== " " && cut > 0)&nbsp; &nbsp; &nbsp; &nbsp; cut--;&nbsp; &nbsp; if(cut == 0) //word longer than 35 chars here&nbsp; &nbsp; &nbsp; cut = 35;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; addr[i] = longAddr.slice(0, cut);&nbsp; &nbsp; longAddr = longAddr.slice(cut + 1);}console.log(addr);它会生成一个包含 5 个元素的数组,并将字符串分割成这些元素。现在您所要做的就是将值放入文本框中
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript