如何从 type="button" 的表单元素获取值

发布于 2024-10-06 03:18:33 字数 467 浏览 1 评论 0 原文

我在表单中有以下代码

<input type="button" name="nameofbutton" value="VALUE 1" onclick="javascript:if (confirm('Are you sure?')) {someSubmitFunction();}"/> 
<input type="button" name="nameofbutton" value="VALUE 2" onclick="javascript:if (confirm('Are you sure??')) {someSubmitFunction();}"/> 

当提交表单时,使用经典 ASP,我尝试使用 Request.Form("nameofbutton") 但没有得到任何值。谁能解释一下为什么吗?如果我将其更改为“提交”类型的按钮,它会起作用,但这意味着表单会提交两次。

谢谢

I have the following code within a form

<input type="button" name="nameofbutton" value="VALUE 1" onclick="javascript:if (confirm('Are you sure?')) {someSubmitFunction();}"/> 
<input type="button" name="nameofbutton" value="VALUE 2" onclick="javascript:if (confirm('Are you sure??')) {someSubmitFunction();}"/> 

When the form is submitted, with classic ASP I try and useRequest.Form("nameofbutton") but I get no value. Could anyone please explain why? if I change it to a button of type "submit", it works, but it means the form gets submitted twice.

Thanks

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

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

发布评论

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

评论(2

我ぃ本無心為│何有愛 2024-10-13 03:18:33

为什么使用输入按钮来提交?

<input type="submit" ... onclick="return confirm('Are you sure?');" />

如果提交按钮的 onclick(或者更好的是,表单的 onsubmit)返回 false,则提交将被取消。

Why are you using input buttons to submit?

<input type="submit" ... onclick="return confirm('Are you sure?');" />

if the onclick of a submit button (or better yet, the onsubmit of a form) returns false, then the submit is cancelled.

聊慰 2024-10-13 03:18:33

@Janusz Jasinski:这对我有用;您会注意到,我故意将按钮的内容设置为whatever,当您提交表单时,value 属性的内容将显示。保存代码并亲自测试一下,这是一个工作示例。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>

<body>

<%
If UCase(Request.ServerVariables("REQUEST_METHOD")) = "POST" Then
    Response.Write "<p>nameofbutton: " & Request.Form("nameofbutton") & "</p>"
End If
%>

<form action="4367350.asp" method="post">
    <div>
        <button name="nameofbutton" value="VALUE 1" onclick="if (confirm('Are you sure?')) { this.form.submit(); } else { return false; }">what ever 1</button>
        <button name="nameofbutton" value="VALUE 2" onclick="if (confirm('Are you sure?')) { this.form.submit(); } else { return false; }">what ever 2</button>
    </div>
</form>

</body>
</html>

@Janusz Jasinski: This works for me; you'll notice I purposely made the contents of the buttons what ever, when you submit the form the value attribute's contents will be shown. Save the code and test it out for yourself, it's a working sample.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>

<body>

<%
If UCase(Request.ServerVariables("REQUEST_METHOD")) = "POST" Then
    Response.Write "<p>nameofbutton: " & Request.Form("nameofbutton") & "</p>"
End If
%>

<form action="4367350.asp" method="post">
    <div>
        <button name="nameofbutton" value="VALUE 1" onclick="if (confirm('Are you sure?')) { this.form.submit(); } else { return false; }">what ever 1</button>
        <button name="nameofbutton" value="VALUE 2" onclick="if (confirm('Are you sure?')) { this.form.submit(); } else { return false; }">what ever 2</button>
    </div>
</form>

</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文