JQuery 问题 - 将值附加到隐藏输入字段?

发布于 2024-10-03 10:27:44 字数 1153 浏览 3 评论 0原文

我有三个表单字段:姓名、名字和姓氏。名称字段被隐藏,我试图在按键时将值设置为名字+姓氏。我还尝试对其进行格式化,使字符全部小写,并将空格转换为破折号。

所以:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我要还你自由 2024-10-10 10:27:44

也许我缺少一些上下文,但我认为您不能使用这样的属性选择器。要获取名字字段,您可以输入:input[name='firstname']

你还会做同样的事情两次,这总是一个需要额外考虑的好理由。例如,您可以一次完成两个 keyup

$("input[name='firstname'], input[name='lastname']").keyup(...);

不过,您可以通过使用 ID 使事情变得更容易。例如,采用以下 HTML:

<input type="text" name="firstname" id="firstname" value="John John" />
<input type="text" name="lastname" id="lastname" value="Doe" />
<input type="hidden" name="name" id="sanitized_name" value="john-john-doe" />

您可以使用以下代码片段:

$('#firstname, #lastname').keyup(function() {
  var fullname = $('#firstname').val() + ' ' + $('#lastname').val();
  $('#sanitized_name').val(fullname.replace(/\s/g, '-').toLowerCase());
});

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 keyups in one shot:

$("input[name='firstname'], input[name='lastname']").keyup(...);

You can make things a lot easier by using IDs, though. For example, take this HTML:

<input type="text" name="firstname" id="firstname" value="John John" />
<input type="text" name="lastname" id="lastname" value="Doe" />
<input type="hidden" name="name" id="sanitized_name" value="john-john-doe" />

You'd use the following code snippit:

$('#firstname, #lastname').keyup(function() {
  var fullname = $('#firstname').val() + ' ' + $('#lastname').val();
  $('#sanitized_name').val(fullname.replace(/\s/g, '-').toLowerCase());
});
呢古 2024-10-10 10:27:44

省略 [0][value] ,如下所示:

$('input[name=field_firstname]') // removed [0][value]

不完全确定为什么将其放在那里。

另外,您可能希望使用 .blur() 而不是 .keyup(),这样可以节省大量冗余重复。更好的是,将字段连接到 .submit() 上,因为无论如何,这就是您关心它的时候。想一想,“Jhon Doe”意味着使用 .keyup() 以及 jQuery 进程来完成它时的 16 次查找和 8 次串联。

附录

现在已经看到过几次了,人们将无效/有问题的字符放入 id 属性中,例如 [0][value],这对 jQuery 来说是无效的,因为它会将其识别为css 表达式而不是 id 的一部分。 document.getElementById( "bla[0]" ); 似乎确实有效,但是如果您确实必须(或者懒得修复它),您可以执行以下操作:

$( document.getElementById( "bla[0][value]" ) ) //etc

Omit [0][value] as in:

$('input[name=field_firstname]') // removed [0][value]

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:

$( document.getElementById( "bla[0][value]" ) ) //etc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文