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

这篇博客介绍了如何使用JavaScript实现三个功能:1) 设置HTML下拉框为只读;2) 判断两个数组是否相等,通过遍历并比较元素;3) 在表单中通过ID为name属性赋值,并注意可能遇到的undefined错误。同时,还展示了获取特定命名模式的输入元素并显示其值的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值