1

我从用户那里得到一个输入,例如他的电话号码是这样的:

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 函数将输入删除一个?

谢谢。

4

6 回答 6

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?
}
于 2012-11-20T08:13:43.427 回答
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;
}
于 2012-11-20T08:15:10.223 回答
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;
  }
于 2012-11-20T08:13:44.013 回答
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;
}
于 2012-11-20T08:14:04.283 回答
1

试试这个:

var input = document.getElementById('number_input');
input.value = input.value.slice(0, -1);
于 2012-11-20T08:36:11.687 回答
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());
           }
       }
于 2014-01-21T07:16:29.883 回答