jQuery/js val() 覆盖 PHPs' $_POST[“字段”]。如何修复?
我有一个非常复杂的(对我来说!)表单验证要做。我做了所有的 js,现在我正在做 php 的东西。
问题是我可以将部分输入复制到其他类似的部分(收件人 -> 付款人)。这一切都是由 jQuery 首先将 $("input.payer_sth").val()
复制到 $("input.payer_sth")
完成的,然后一次又一次地执行关于键盘和模糊。
我所有的输入都是这样构建的:
<input id="payer_name" name="payer_name" class="foo" type="text" value="<?=$_POST['payer_name']?>"/>
只要那些没有被 jQuery 修改的输入在提交和“返回”时工作正常,那些修改了 val()
的输入在返回时都是空的。
对我来说显而易见的是 jQuery 正在覆盖 value=""
。
如何解决这个问题?
I've got a pretty complicated (for me!) form validation to do. I did all js and now I'm doing php stuff.
The thing is I've put a possibility to copy part of the inputs to other, similar section (recipient -> payer). It's all done by jQuery first copying $("input.payer_sth").val()
to $("input.payer_sth")
, and then doing it again and again on keyup and blur.
All my inputs are built like that:
<input id="payer_name" name="payer_name" class="foo" type="text" value="<?=$_POST['payer_name']?>"/>
as long as the ones that aren't modified by jQuery work all right on submit and "back", the ones that has modified val()
are empty on back.
What's obvious for me is that jQuery is overwriting value="<?=$_POST['field']?>"
.
How can this be fixed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我上面的评论,您可能想在服务器端而不是客户端执行此操作。
示例(不是最干净的,但你会明白的)
所以这会做什么,如果没有设置帖子,它将在字段中输出“输入你的名字”,如果设置了 $_POST 它将输出值的职位进入该领域。
该构造称为三元运算符: http://php.net/manual/ en/language.operators.comparison.php
你可以自己清理一下,但这可以解决问题,并且可以避免在不需要 JS 的情况下使用 JavaScript最终用户。
Based on my comments above, you may want to do this on server-side instead of client side.
Example (not the cleanest, but you'll get the point)
So what this will do, is if there is no post set it will output "Enter your name" into the field, if the $_POST is set it will output the value of the post into the field.
The construct is called a ternary operator: http://php.net/manual/en/language.operators.comparison.php
You can clean it up a bit on your side, but this will do the trick and it'll avoid the use of JavaScript for something like this where JS should not be required by the end-user.