function getValues() //获取文本
{
var oTextBox1=document.getElementById(”txt1″);
var oTextBox2=document.getElementById(”txt2″);
alert(”The value of txt1 is /”" + oTextBox1.value + “/n” + “The value of txt2 is /”" + oTextBox2.value + “/”");
}
function SelectText() //选择文本
{
var TextBox1=document.getElementById(”txt1″);
TextBox1.focus();
TextBox1.select();
}
function SetValues()
{
var TextBox1=document.getElementById(”txt1″);
TextBox1.value=”Hello World”;
//TextBox1.select();
}
function GetStringLength() //获取字符串的长度
{
var TextBox1=document.getElementById(”txt1″);
alert(TextBox1.value.length);
}
//文本框事件
//change和blur事件的区别:
//
//change:当用户更改内容后文本框失去焦点时发生(如果是通过value特性来更改内容,则不触发)
//blur:只要文本框失去焦点就会触发该事件
//如果文本不变,但是文本框失去焦点,那么只有blur事件触发;如果文本发生了变化,则先触发change事件,然后触发blur事件。
//select事件,当用户选中一个或者多个字符或调用select方法时触发这个事件
//自动选择文本
//FormUtil.setTextBoxsSelected=function()
function setTextBoxsSelected()
{
var inputs=document.getElementsByTagName(”input”);
var textAreas=document.getElementsByTagName(”textarea”);
for(var i=0;i<inputs.length;i++)
{
if(inputs[i].type ==”text”|| inputs[i].type ==”password”)
{
inputs[i].οnfοcus=function(){ this.select(); }
}
}
for(var i=0;i<textAreas.length;i++)
{
textAreas[i].οnfοcus=function(){ this.select();}
}
};
//自动切换到下一个
//这个方法必须在每个字符输入后调用,所以需要使用onkeyup事件
//
//案例:aaa-bbb-vvvvvvv的输入格式
function tabForward(txt)
{
var txtForm=txt.form;
//判断是否为最后一个
if(txtForm.elements[txtForm.elements.length-1]!= txt && txt.value.length == txt.getAttribute(”maxlength”))
{
for(var i=0;i<txtForm.elements.length;i++)
{
if(txtForm.elements[i]==txt)
{
for(var j=i+1;j<txtForm.elements.length;j++)
{
if(txtForm.elements[j].type!=”hidden”)
{
txtForm.elements[j].focus();
return ;
}
}
}
}
}
}
//限制textArea的字符数
//由于该控件没有maxlength特性,解决的办法是使用javascript来模拟maxlength特性
//禁止插入字符,使用的是onkeypress事件
function textAreaIsNotMax(otextArea)
{
return otextArea.value.length!=otextArea.getAttribute(”maxlength”);
}
//允许和阻止文本框中的字符
//<input type=”text” invalidchars=”0123456789″ />可以直接阻止0~9这几个数字
JavaScript在文本框中的技术
最新推荐文章于 2024-07-21 10:17:51 发布