gpt4 book ai didi

javascript - JavaScript 中的切片函数

转载 作者:行者123 更新时间:2023-11-28 07:13:12 24 4
gpt4 key购买 nike

我的场景如下:我将从文本框中输入字符串,一旦用户单击提交按钮,这将分割一些字符。这是我迄今为止的工作:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function Check()
{
var inputBox = document.getElementById("TextBox1").value;
if (inputBox == "") {
alert("Enter your full name.");
}

var str = inputBox.value;
var result = str.slice(1, 4);
alert(result);
}
</script>
</head>
<body>
<form id="form2" runat="server">
<div>
<table>
<tr>
<td>Enter your full name: </td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td><button type="button" onclick="Check()" >Submit</button></td>
</tr>
</table>
</div>
</form>
</body>
</html>

最佳答案

给你。 jQuery 代码:

<asp:TextBox ID="TextBox1" ClientIDMode="Static" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Substring(1,4)" />
<script type="text/javascript">
$(document).ready(function () {
$('#<% = Button1.ClientID %>').click(function (e) {
$('#<% = TextBox1.ClientID %>').val(
$('#<% = TextBox1.ClientID %>').val().substring(1, 4));
e.preventDefault(); // prevent PostBack to Server
});
});
</script>

关于javascript - JavaScript 中的切片函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31098180/

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