如何从 type="button" 的表单元素中获取值 [英] How to get value from a form element of type="button"

查看:85
本文介绍了如何从 type="button" 的表单元素中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单中有以下代码

<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();}"/>

当表单提交时,我尝试使用经典的 ASPRequest.Form("nameofbutton") 但我没有得到任何价值.任何人都可以请解释为什么?如果我将其更改为提交"类型的按钮,它会起作用,但这意味着表单被提交了两次.

谢谢

解决方案

@Janusz Jasinski:这对我有用;您会注意到我故意将按钮的内容设为 what ever,当您提交表单时,将显示 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"><头><title></title><身体><%If UCase(Request.ServerVariables("REQUEST_METHOD")) = "POST" ThenResponse.Write "<p>nameofbutton: " &Request.Form("nameofbutton") &</p>"万一%><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; }">1<button name="nameofbutton" value="VALUE 2" onclick="if (confirm('Are you sure?')) { this.form.submit(); } else { return false; }">2

</表单></html>

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

解决方案

@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>

这篇关于如何从 type="button" 的表单元素中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆