显示与输入不同的值,该值将作为php接收 [英] Show a different value from an input that what will be received as php

查看:92
本文介绍了显示与输入不同的值,该值将作为php接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以在文本输入中显示与发送给PHP的值不同的值.

I'd like to know if there is a way to show in an input of type text a different value than the one send to PHP.

例如,假设您拥有:

<input type="text" value="John">显示约翰.

我可以为用户将值更改为"Doe",但对于php保留为"John"吗?

Can I change the value to "Doe" for the user but keep it to "John" for php?

我可以利用$.attr('value')$.val()之间的区别来实现这一点吗?

Can I achieve this using the difference between $.attr('value') and $.val()?

我进行了一些测试,看来我将不得不直接在我的控制器中对其进行逆转.还有其他解决方案吗?

I ran a couple of tests and it seems that I will have to reverse it directly in my controller. Is there an other solution?

这里有一些 jSFiddle 可以玩.

推荐答案

一个奇怪的请求可以确定,但是...

An odd request to be sure but...

您不能更改字段的值,而只能进行简单的表单提交.该字段将忠实地发送其中的任何内容.围绕这种思想有一些骇人听闻的方式

You can't change the field's value and just do a simple form submission. The field will dutifully send whatever is in it. There's a few hackery ways around this tho

因此,创建一个字段,disable,然后添加一个隐藏字段.禁用的字段永远不会成功,尽管用户将无法更改字段值,并且许多浏览器会自动更改字段的样式

So make a field, disable it, and add a hidden field. Disabled fields are never successful, although the user will be unable to change the field value and many browsers will change the styling of the field automatically

<input type="text" name="name" value="John" disabled>
<input type="hidden" name="name" value="Doe">

选项2-更改提交值

如上所述,您始终可以在提交表单时更改该值.下面的侦听器将捕获您使用jQuery提交的表单,因为您是这样询问的

Option 2 - Change the value on submit

As you mentioned, you can always change the value when the form is submitted. The below listener will capture the form submitt Using jQuery since you asked it that way

$("form").on( "submit", function( event ) {
  event.preventDefault();
  $('input[name="name"]').val('Doe');
  $("form").submit();
});

这篇关于显示与输入不同的值,该值将作为php接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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