gpt4 book ai didi

javascript - 如果值未更改,如何在提交时删除不需要的输入字段中的默认值?

转载 作者:搜寻专家 更新时间:2023-10-31 08:11:55 24 4
gpt4 key购买 nike

默认情况下,我有一个输入字段的值,告诉用户在那里输入什么(例如输入您的帐号)。我有一个脚本,当你点击它时它会隐藏值,当你点击离开时它会重新出现。

虽然该字段不是必需的。如果用户没有在该字段中输入信息,则会发送默认值并导致错误,因为它只允许数字。如果用户不填写账号,这个默认值怎么改成nothing?

最佳答案

不要使用默认值。使用 placeholders .

<input type="text" placeholder="Real Name">

Works in all decent browsers.


或者,在服务器端忽略这些值:

if ($_POST["realname"] == "Real Name") { real_name_not_provided(); }

如果你仍然坚持使用 JavaScript(为了更好的浏览器支持),你可以使用类似下面的东西:

 $(function() {
if(!$.support.placeholder) {
var active = document.activeElement;
$(':text').focus(function () {
if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
}).blur(function () {
if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
});
$(':text').blur();
$(active).focus();
$('form').submit(function () {
$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
});
}
});

Taken from this article (which was first on Google by the way) 。请在使用此代码之前阅读文章。它可能需要文章中的更多代码。

关于javascript - 如果值未更改,如何在提交时删除不需要的输入字段中的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10018772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com