簡體   English   中英

用戶開始鍵入時如何清除表單的默認值(jQuery)

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

當用戶開始鍵入時,如何清除要清除的表單字段(帶有默認文本“ 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屬性。

如果您特別不希望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> 

如果我正確理解了您的問題,請看一下該線程

對您來說,它將像:

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

如果您確實要在輸入中輸入文本而不是placeholder但您的問題不清楚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM