1.遍历select
for(var i=0;i<document.getElementById(id).options.length;i++)
{
if(document.getElementById(id).options[i].value == val)
{
document.getElementById(id).options[i].selected=true;
break;
}
}
2.字符串是否包含某字符
返回 String对象内第一次出现子字符串的字符位置。 strObj.indexOf(subString[, startIndex]) 参数 :strObj 必选项。String对象或文字。 subString 必选项。要在String 对象中查找的子字符串。 starIndex 可选项。该整数值指出在String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。
完全匹配:
if(types.indexOf(content)==0){
docType.options[i].selected=true;
}
包含:
if(types.indexOf(content)!=-1){
docType.options[i].selected=true;
}
3.判断字符串是否为空
var strings = '';
if (string.length == 0)
{
alert('不能为空');
}
4.判断字符串是否为“空”字符,即输入了空格
var strings = ' ';
if (strings.replace(/(^s*)|(s*$)/g, "").length ==0)
{
alert('不能为空'); }
5.给input加载onchange事件
var obj=document.getElementById("ID");
obj.οnchange=function(){
...
}