如何在用户开始键入(jquery)时清除表单的默认值

How to clear default value of form when the user starts typing (jquery)

本文关键字:清除 表单 默认值 jquery 用户 开始      更新时间:2023-09-26

当用户开始键入时,如何清除表单字段(默认文本为"default")?我在下面附上了我的代码。我希望它具有与.prompt()命令类似的功能。

<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Dialog - Modal form</title>
<script src="jquery-2.1.4.min.js"></script>
<script src="jquery-ui.js"></script>
<link href="css.css" rel="stylesheet" type="text/css" />
<link href="jquery-ui.css" rel="stylesheet" type="text/css" />
<style>
</style>
<script>
    $ (function() {
        var dialog;
        dialog = $("#dialog-form").dialog({
            autoOpen: false,
            height:300,
            width:350,
            modal:true,
        });
        form = dialog.find( "form" ).on( "submit", function( event ){
            event.preventDefault();
            $( "#placeName" ).append(name.val);
        }); 
        $ ( "#clickMe" ).button().on("click", function(){
            dialog.dialog("open");
        }); 

    });

</script>

这是表单的制作位置,值设置为"默认"

  <body>    
      <div id="dialog-form" title="create user"> <!--starts out hidden-->
        <form>
            <input type="text" name="name" id="name" value="default" class="text ui-widget-content ui-corner-all">
        </form>
    </div>
    <button id="clickMe">Click!</button>
    <br><br><hr><br><br>
    <div id="placeName"></div>

</body>
</html>

<input {Other attributes} placeholder="default" />

但是,您需要删除value属性。

如果我正确理解了你的问题,我会看看这个线程。

对你来说,这就像:

$('input').on('click focusin', function() {
    this.value = '';
});

如果你真的想在你的输入中有一个文本,而不是placeholder,但在你的问题中不清楚。

如果您特别不想要placeholder,而是想要value

<div id="dialog-form" title="create user"> <!--starts out hidden-->
        <form>
            <input type="text" name="name" id="name" value="default"
 class="text ui-widget-content ui-corner-all" onfocus="this.onfocus=null;this.value=''" onblur="this.value=this.value||'default'">
        </form>
    </div>