js相关1.设置下拉框只读2.判断两个数组是否相等3.form表单取值

来源:互联网 发布:云计算工程师发展前景 编辑:程序博客网 时间:2024/05/16 11:41

1.设置选中下拉框只读

<select id="select">  

    <option>bbb</option>  
    <option>aaa</option>
    <option selected>ccc</option>   
</select>  
<script type="text/javascript">  
SetReadOnly(document.getElementById("select"));  
    function SetReadOnly(obj){  
        if(obj){  
            obj.onbeforeactivate = function(){return false;};  
            obj.onfocus = function(){obj.blur();};  
            obj.onmouseover = function(){obj.setCapture();};  
            obj.onmouseout = function(){obj.releaseCapture();};  
        }  
    }  

</script>

2.判断两个数组是否相等

<body>
<div id="mydiv">
</div>
</body>
<script type="text/javascript">
    var arrayone = ["11","3","5"];
    var arraytwo = ["1",3,5];
    var compare = function(arrayone,arraytwo){
        if(arrayone!=null&&arrayone.length>0&&arraytwo!=null&&arraytwo.length>0&&arrayone.length===arraytwo.length){
            for(var i=0;i<arraytwo.length;i++){
                if(arrayone.indexOf(arraytwo[i])===-1){
                        //alert("两个数组不相等!");
                        document.getElementById("mydiv").innerHTML="两个数组不相等!";
                        return false;
                }
            }
        }        
    }
//调用
compare(arrayone,arraytwo);
</script>

3.form表单通过id为name属性赋值

注意如果name值为name时会报undefined错误.使用时注意

<form name="myform" id="myform">
    <input type="text" name="username" id="username"/>
    <input type="button" value="提交" id="but" onclick="myFunction()" />
</form>

<script type="text/javascript">

function myFunction(){
        myform.username.value = username.value;
        alert(myform.username.value);
        //alert("aaa");
    }

</script>

------------------------------------------------------------

<input name="sort01" value="">
<input name="sort02" value="">
<input name="sort03" value="">

<script type="text/javascript">
var input = document.getElementsByTagName("input");
     for(var i = 0,l = input.length;i < l;i++){
         if(/^short/.test(input[i].name)){
            alert(input[i].name+':'+input[i].value);
         }
     }
</script>

原创粉丝点击