限制"提示"框中的输入

Luc*_*ucy 6 html javascript jquery

我希望我的提示框最多输入10个字符.如果用户按任意键,则不应输入任何文本.

目前的代码,我使用用途而条件检查输入是否在limit.If它是超越限制它接受的文本,但不断提示用户,直到输入内limit.The值将被分配到PERSON1变量和代码只有当输入在限制范围内时才会继续前进.

我希望输入框不接受第一个位置本身的指定输入.

请帮忙.

代码如下:

      person1=prompt("Please enter your data 1(Maximum limit 20)","Sample 1");
      while(person1.length > 20){
         alert("Keep the message length to 20 chars or less")
         person1 = prompt("Please enter your data 1(Maximum limit 20)","Sample 1");
        }
Run Code Online (Sandbox Code Playgroud)

Luc*_*ucy 1

使用提示框无法完全解决这个问题。要解决这个问题就必须制作一个jQuery UI 自定义对话框

基本上,您在表单标签内创建一个文本框和两个按钮“确定”和“取消”,并将代码嵌入到 Jquery 中,使它们像提示框一样弹出。

我已使用此页面上具有默认功能的对话框http://jqueryui.com/dialog/#default作为我的基础...

代码的解释在最后给出。

这是执行此操作的完整代码...

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Dialog - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script>
$(document).ready(function ()
{   
    $('#dialog').dialog({
        autoOpen: false,
        width: 250,
        height: 180,
        modal : true,
        resizable: false,
    show:"slow"
    });

$('#put').click(function() {
       $( "#dialog" ).dialog( "open" );
   });
 });

 function getPdata( arg ) {             //Function called when Cancel button is pressed
 var f = document.getElementById( 'pForm' );
  // exit immediately
 close();
 return;
 }

function close(){
$( "#dialog" ).dialog( 'close' );
 }

 var cnt=1;
  function getPdata1() {       //function called when ok button is pressed
  var f = document.getElementById( 'pForm' );
  var n = $("input[name='name']").val();

  alert(n.length);
  if (n.length <= 10) {
       $('<div />',{id:"div" + cnt }).html(n).appendTo('body');  //crated div will have an id as `div`+ count;
  } else {
        alert('the data you have enterd is not in limit.Please try again');
         return;

  }
    close();
    if (cnt < 5) {
         f.name.value = "";
          $("#dialog").dialog('open');
          cnt++;
    }

}

function show()
   {
    document.getElementById("but1").style.visibility="visible";    

    }


     </script>
     </head>
     <body>

     <div>
    <button type="button" id="put" >Insert data</button>
    </div>

    <div id="dialog" title="Input Data"> <!--the actual contebts of dialog box-->
    <form id="pForm" >
     name: <input type="text" name="name" width='50' height='100' maxlength="10"   placeholder="Fill in your data" /><br><br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="button" value="OK" onclick="getPdata1()" />
    <input type="button" value="cancel" onclick="getPdata( this.value )" />
   </form>
   </div> 
   </body>
    </html>
Run Code Online (Sandbox Code Playgroud)

代码解释..

  • 网页上会显示一个按钮。单击时将显示一个弹出对话框,其中包含一个文本框和 2 个命令按钮。
  • 输入已限制为 10 个字符。
  • 所输入的内容将作为输出打印在按钮下方的网页上。
  • 由于计数器已指定,因此将进行 5 次输入,直到 5..

上面的代码完全可以根据个人需要修改。希望这个答案对你有帮助。如有疑问请随时询问。