找到你要的答案

Q:HTML button/input I want to keep different text and value and read value in server

Q:HTML按钮/输入我想保持不同的文本和值和服务器读取值

I tried googling this but didn't get a proper answer, so here goes the question to the experts on this forum:

I want to render a button on my web page such that it's value is different from what is displayed on screen. e.g if I use below html tag, Confirm is displayed.

<button name="type" value="save">Confirm</button>

However I want to read value "save" in the server so that I can have certain logic based on that. If I use

request.getParameter("type");

I get "Confirm" instead of "save".

My objective is to have multiple buttons with different values and at server side I want to know what button was clicked. I don't want to link the server code with the displayed text.

I can do a workaround such that onsubmit I call a javascript function that captures the button clicked and puts "save" in hidden field with name="type". But this seems like such a common client-server problem that there must be a more elegant solution to this problem that I am not aware of.

Appreciate any help.

我试着搜索了但没有得到正确的答案,所以这里去这个论坛上专家的问题:

我想在我的网页上显示一个按钮,这样它的值与屏幕上显示的不同。如果我使用下面的HTML标签,确认显示。

<button name="type" value="save">Confirm</button>

但是我想在服务器上读取值“保存”,这样我就可以有一定的逻辑基础。如果我用

request.getParameter("type");

我得到“确认”而不是“保存”。

我的目标是有多个不同值的按钮,并在服务器端,我想知道什么按钮被点击。我不想把服务器代码与显示的文本联系起来。

我可以做一个变通,onsubmit我叫一个JavaScript函数捕捉按钮并“保存”在隐藏字段名称=“型”。但这似乎是这样一个常见的客户机服务器问题,必须有一个更优雅的解决这个问题,我不知道。

感谢任何帮助。

answer1: 回答1:

Can you post more of your code? Both the HTML form and the server side code? The behavior you are expecting IS in fact the expected behavior for the code you have shown. I suspect you have a bug elsewhere in your code (like maybe reusing the variable name somewhere).

你能张贴更多的代码吗?无论是HTML表单和服务器端代码?您期望的行为实际上是您所示代码的预期行为。我怀疑你的代码中有一个bug(比如在某处重用变量名)。

answer2: 回答2:

It would make sense that <button> works such that value attribute is sent in POST request whereas the displayed text is whatever is between <button>...</button>.

Unfortunately, standards don't define this behavior, so the browsers are left to implement it the way they want. In Firefox, value attribute is posted. But in Internet Explorer (I know about IE8, don't know if it changed in later version), value is not posted. Instead displayed text is posted!

In a nutshell you have to workaround this problem. There are a couple of good suggestions posted here.

This is what I found most useful or worked for me the most. Add a hidden field with the same name as I intend to use in the server.

<input type="hidden" name="type" id="btntype" />
<button type="submit" name="submitbtn" value="save">Confirm</button>
<button type="submit" name="submitbtn" value="verify">Verify</button>
<button type="submit" name="submitbtn" vallue="cancel">Cancel</button>

And this in Javascript:

$(document).ready(function() {
   $(":submit[name='submitbtn']").on("click", function() {
      $("#btntype").val($(this).val());
   });
});

Now, in the servlet, I can use:

request.getParameter("type");

It will give value save if Confirm is clicked, verify if Verify is clicked and cancel if Cancel button is clicked.

它使得<;按钮>;作品价值属性是POST请求而显示的文本是在& lt送;按钮& gt;…& lt;/按钮& gt;。

不幸的是,标准没有定义这种行为,所以浏览器被留下来实现它的方式,他们想要。在Firefox中,值属性被发布。但在Internet Explorer(我知道IE8,不知道这改变之后的版本),价值不贴。显示文本!

简而言之,你必须解决这个问题。这里有几个好的建议。

这是我发现最有用或最为我工作的。添加一个与我打算在服务器中使用的名称相同的隐藏字段。

<input type="hidden" name="type" id="btntype" />
<button type="submit" name="submitbtn" value="save">Confirm</button>
<button type="submit" name="submitbtn" value="verify">Verify</button>
<button type="submit" name="submitbtn" vallue="cancel">Cancel</button>

这在JavaScript:

$(document).ready(function() {
   $(":submit[name='submitbtn']").on("click", function() {
      $("#btntype").val($(this).val());
   });
});

现在,在servlet,我可以使用:

request.getParameter("type");

如果单击“确认”按钮,它会给“值”保存,如果单击“取消”按钮,则验证是否单击“取消”。

answer3: 回答3:

This way also it will work. Just define the id for button.Whenever that particular submit button will be clicked just call .on() method

<button id="filter" value="value1" type="submit">Confirm</button>
<button id="filter" value="value2" type="submit">Verify</button>
<button id="filter" value="value3" type="submit">Cancel</button>

$(window).on("click", "#filter", function() {
        alert($(this).val());
    });

这样也会起作用。只要定义ID按钮。每当特定的提交按钮将点击打on()方法。

<button id="filter" value="value1" type="submit">Confirm</button>
<button id="filter" value="value2" type="submit">Verify</button>
<button id="filter" value="value3" type="submit">Cancel</button>

$(window).on("click", "#filter", function() {
        alert($(this).val());
    });
java  html  servlets  button  input