【问题标题】:Replace default placeholder after form submit表单提交后替换默认占位符
【发布时间】:2013-04-15 23:18:18
【问题描述】:

如何将表单中的默认占位符(使用 JQuery 或 JavaScript)替换为提交的值?谢谢!

这是一个示例代码:

<form name="" id="RegForm" method="post" action="">
<table cellpadding="0" cellspacing="0" width="260" align="center">
    <tr>
        <td align="center" colspan="2" height="45" valign="middle"><input type="text" name="username" id="username" class="text" placeholder="Your Username here" style="background:url(img/bg_text.png) no-repeat;" /></td>
    </tr>
    <tr>
        <td align="left" width="120" height="50" valign="middle"><input type="image" id="submitbtn" src="img/but_register.png" class="submit" onclick="checkPreReg()"/></td>
    </tr>
</table>

Javascript:

<script>
   function checkPreReg() { 
    var form = $("#RegForm");
    var u = $("#username", form).val();
    $('#username',form).attr('placeholder',u);
}
</script>

它根本不起作用。我尝试了其他脚本,但在新的占位符短暂出现后,该值又回到了默认占位符。

【问题讨论】:

  • 我认为把它作为占位符会让人困惑,最好把它作为默认值。
  • 我不介意使用“值”而不是“占位符”,只要我得到功能,即:a)该字段告诉输入什么(即用户名)b)当用户在该字段中单击,默认文本消失 c) 当用户提交表单时,该字段显示用户输入的内容。谢谢你,罗伯!
  • 输入类型的图像是一个提交按钮。表单正在提交,刷新页面以便您看到原始表单。不要过度使用占位符,也不要使用它们代替适当的标签。

标签: javascript replace default placeholder


【解决方案1】:

我会说这个问题可能需要一些澄清,但我会试一试。

如果你的意思是:

<input type="text" placeholder="default_placeholder" />

那我试试:

$(document).ready(function(){
  $('#FORM').submit(function(){
    $('#INPUT_ID').attr('placeholder','new text here');
  });
});

【讨论】:

  • @VincentHirth — 它“有效”,但与您预期的不同,您只是看不到结果,因为表单已提交。
猜你喜欢
  • 1970-01-01
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多