JQuery 问题 - 将值附加到隐藏输入字段?
我有三个表单字段:姓名、名字和姓氏。名称字段被隐藏,我试图在按键时将值设置为名字+姓氏。我还尝试对其进行格式化,使字符全部小写,并将空格转换为破折号。
所以:
<input type="text" name="firstname" value="John John" />
<input type="text" name="lastname" value="Doe" />
<input type="hidden" name="name" value="john-john-doe" />
我一直在尝试使用以下代码,但它不起作用......
$('input[name=field_firstname[0][value]]').keyup(function() {
var firstname = $('input[name=field_firstname[0][value]]').val();
var lastname = $('input[name=field_lastname[0][value]]').val();
$('input[name=name]').val(firstname.replace(/\s/g, "-").toLowerCase()+lastname.replace(/\s/g, "-").toLowerCase());
});
$('input[name=field_lastname[0][value]]').keyup(function() {
var firstname = $('input[name=field_firstname[0][value]]').val();
var lastname = $('input[name=field_lastname[0][value]]').val();
$('input[name=name]').val(firstname.replace(/\s/g, "-").toLowerCase()+lastname.replace(/\s/g, "-").toLowerCase());
});
I have three form fields: name, first name, and lastname. The name field is hidden and I'm trying to set the value to the firstname + lastname on keypress. I'm also trying to format it so that the characters are all lowercase and spaces are transformed into dashes.
So:
<input type="text" name="firstname" value="John John" />
<input type="text" name="lastname" value="Doe" />
<input type="hidden" name="name" value="john-john-doe" />
I've been trying to use the following code, but it's not working...
$('input[name=field_firstname[0][value]]').keyup(function() {
var firstname = $('input[name=field_firstname[0][value]]').val();
var lastname = $('input[name=field_lastname[0][value]]').val();
$('input[name=name]').val(firstname.replace(/\s/g, "-").toLowerCase()+lastname.replace(/\s/g, "-").toLowerCase());
});
$('input[name=field_lastname[0][value]]').keyup(function() {
var firstname = $('input[name=field_firstname[0][value]]').val();
var lastname = $('input[name=field_lastname[0][value]]').val();
$('input[name=name]').val(firstname.replace(/\s/g, "-").toLowerCase()+lastname.replace(/\s/g, "-").toLowerCase());
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许我缺少一些上下文,但我认为您不能使用这样的属性选择器。要获取名字字段,您可以输入:
input[name='firstname']
。你还会做同样的事情两次,这总是一个需要额外考虑的好理由。例如,您可以一次完成两个
keyup
:不过,您可以通过使用 ID 使事情变得更容易。例如,采用以下 HTML:
您可以使用以下代码片段:
Perhaps I'm missing some context, but I don't think you can use attribute selectors like that. To get at the firstname field, you'd say:
input[name='firstname']
.You're also doing the same thing twice, which is always a good reason to give things some extra thought. For example, you can do both
keyup
s in one shot:You can make things a lot easier by using IDs, though. For example, take this HTML:
You'd use the following code snippit:
省略
[0][value]
,如下所示:不完全确定为什么将其放在那里。
另外,您可能希望使用
.blur()
而不是.keyup()
,这样可以节省大量冗余重复。更好的是,将字段连接到.submit()
上,因为无论如何,这就是您关心它的时候。想一想,“Jhon Doe”意味着使用.keyup()
以及 jQuery 进程来完成它时的 16 次查找和 8 次串联。附录
现在已经看到过几次了,人们将无效/有问题的字符放入 id 属性中,例如
[0][value]
,这对 jQuery 来说是无效的,因为它会将其识别为css 表达式而不是 id 的一部分。document.getElementById( "bla[0]" );
似乎确实有效,但是如果您确实必须(或者懒得修复它),您可以执行以下操作:Omit
[0][value]
as in:Not entirely sure why you put that in there.
Also, you might want to use
.blur()
instead of.keyup()
, saves you a lot of redundant repetition. Even better, concat the fields on a.submit()
because thats when you care about it anyways. Think about it, "Jhon Doe" means 16 lookups and 8 concatenations when using.keyup()
along with jQuery processes to get it done.addendum
Seeing this a few times now, people put invalid/problematic characters inside id attributes like
[0][value]
, this is invalid to jQuery as it recognises it as a css expression rather then part of an id.document.getElementById( "bla[0]" );
does seem to work however so if you really have to (or are too lazy to fix it), you can do the following: