ASP/Javascript - 将 ASP 值发送到 Javascript 函数

发布于 2024-12-05 18:27:40 字数 1363 浏览 1 评论 0原文

是否可以使用 ASP 脚本中的值调用 JS 函数?

我认为这是可能的,所以我尝试了以下方法,但我似乎无法让它运行。正如您所看到的,我在 JS 调用中有一个 ASP 变量“totalPages”。

Response.Write "<form name=""pageSkip"" onsubmit=""return validatePageSkip("&totalPages&")"">"

然后我就有了 JS 函数,即:

function validatePageSkip(totalPages)
{
     if (document.pageSkip.pn.value.length > totalPages)
     {
         alert("Please enter a page number within range!");
         document.pageSkip.pn.focus();
         return(false);
     }
     document.pageSkip.submit();
}
  1. 这可能吗?
  2. 如果是,我做错了什么?

更新

当我使用空文本框提交时,仅使用此功能甚至无法工作:

Response.Write "<form name=""pageSkip"" id=""pageSkip"" onsubmit=""return validatePageSkip()"" method=""post"" action=""?cpr="&pageRange&"#main"">"
Response.Write "<input type=""text"" name=""cpn"" id=""cpn"" spellcheck=""false"" autocomplete=""off"" size=""3"" class=""pageSkipBox"" value="""&currentPage&""">"
Response.Write "</form>"

使用这个简单的 JS 函数:

function validatePageSkip()
{
if (document.pageSkip.cpn.value.length < 1)
{
alert("Test!");
document.pageSkip.cpn.focus();
return(false);
}
document.pageSkip.submit();
}

我真的不知道出了什么问题。表单完美提交,但它只是不会调用该函数。

你能发现什么吗?

Is it possible to call a JS function, with a value from an ASP script?

I thought it was possible, so I tried the following method but I can't seem to get it to run. As you can see, I have an ASP variable "totalPages" in the JS call.

Response.Write "<form name=""pageSkip"" onsubmit=""return validatePageSkip("&totalPages&")"">"

I then have the JS function, which is:

function validatePageSkip(totalPages)
{
     if (document.pageSkip.pn.value.length > totalPages)
     {
         alert("Please enter a page number within range!");
         document.pageSkip.pn.focus();
         return(false);
     }
     document.pageSkip.submit();
}
  1. Is this possible?
  2. If yes, what am I doing wrong?

UPDATE

It won't even work just using this, when I submit with an empty text box:

Response.Write "<form name=""pageSkip"" id=""pageSkip"" onsubmit=""return validatePageSkip()"" method=""post"" action=""?cpr="&pageRange&"#main"">"
Response.Write "<input type=""text"" name=""cpn"" id=""cpn"" spellcheck=""false"" autocomplete=""off"" size=""3"" class=""pageSkipBox"" value="""¤tPage&""">"
Response.Write "</form>"

With this simple JS function:

function validatePageSkip()
{
if (document.pageSkip.cpn.value.length < 1)
{
alert("Test!");
document.pageSkip.cpn.focus();
return(false);
}
document.pageSkip.submit();
}

I really don't know what is wrong. The form submits perfectly but it just won't call the function.

Can you spot anything?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欲拥i 2024-12-12 18:27:40

是的,这是可能的。但你需要确保你正确地转义你的引号

Response.Write "<form name=""pageSkip"" onsubmit=""return validatePageSkip("&totalPages&")"">"

我个人更喜欢在头脑中做这样的事情

<head>
   <script type="text\javascript">
     var totalpages = <%=totalPages%>;
   </script>
</head>

然后

Response.Write "<form name=""pageSkip"" onsubmit=""return validatePageSkip(totalpages)"">"

Yes it is possible. but you need to make sure you escape your quotes correctly

Response.Write "<form name=""pageSkip"" onsubmit=""return validatePageSkip("&totalPages&")"">"

Personally I would prefer doing something like this in the head

<head>
   <script type="text\javascript">
     var totalpages = <%=totalPages%>;
   </script>
</head>

Then

Response.Write "<form name=""pageSkip"" onsubmit=""return validatePageSkip(totalpages)"">"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文