如何逐个删除文本框中的字符?

时间:2012-11-20 08:07:43

标签: javascript html cordova

我正在收到用户的意见,例如他的电话号码:

HTML:

   <input id="number_input"  type="text" size="12" maxlength="19" />

JavaScript的:

    var i = 0;
    var phone_number = [];
    document.getElementById('number_input').value+=digit;
    phone_number[i]= digit ;
    i = i+1;

并允许用户删除当前输入:

   function delete_num ()
  {
     document.getElementById('number_input').value=' ';
     phone_number.length=0;
     return false;
  }

但是在这种情况下我将删除所有输入数据,如何通过激活delete_num函数来删除输入?

感谢。

6 个答案:

答案 0 :(得分:3)

这应该像[Backspace]按钮一样,从number_input字段中删除最后一个字符。

function delete_num ()
{
    var field = document.getElementById('number_input');
    field.value = field.value.slice(0, -1); //Extract from index 0 to the before-last character
    phone_number.pop(); //Remove the last element from the phone number. It's length is maintained by js itself.
    return false; //What's this for?
}

答案 1 :(得分:3)

function delete_num() {
  var input, num;
  input = document.getElementById('number_input');
  num = input.value;
  input.value = num.slice(0,num.length-1);
  return false;
}

答案 2 :(得分:1)

假设这是您所需要的:

function delete_num ()
  {
     var val = document.getElementById('number_input').value;
     val = val.susbtr(0,val.length - 1);
     phone_number.length = val.length;
     document.getElementById('number_input').value = val;
     return false;
  }

答案 3 :(得分:1)

假设您正在重新创建退格功能......

function delete_num()
{
   var text = document.getElementById('number_input').value;
   document.getElementById('number_input').value = text.substring(0, text.length - 2);
   phone_number.length = 0; //not sure what this is...
   return false;
}

答案 4 :(得分:1)

试试这个:

var input = document.getElementById('number_input');
input.value = input.value.slice(0, -1);

答案 5 :(得分:0)

         function RemoveVal(events) {
           var objectControl = $find("<%= txtDescription.ClientID %>");
           var c = objectControl.get_value();
           if (c[0] == "%") {
             var shortenedString = c.substr(c[0],'');
            $find("<%= txtDescription.ClientID %>").set_value(shortenedString.toString());
           }
       }