js 获取select对象

js 获取select对象,第1张

1、获取选中select的value和text,html代码如下:

<select id="mySelect">

<option value="1">one</option>

<option value="2">two</option>

<option value="3">three</option>

</select>

则可通过以下script代码s来获取选中的value和text

$("#mySelect")val(); //获取选中记录的value值

$("#mySelect option:selected")text(); //获取选中记录的text值

2、运用new Option("文本","值")方法添加选项option

var obj = documentgetElementById("mySelect");

objadd(new Option("4","4"));

3、删除所有选项option

var obj = documentgetElementById("mySelect");

objoptionslength = 0;

4、删除选中选项option

var obj = documentgetElementById("mySelect");

var index = objselectedIndex;

objoptionsremove(index);

5、修改选中选项option

var obj = documentgetElementById("mySelect");

var index = objselectedIndex;

objoptions[index] = new Option("three",3); //更改对应的值

objoptions[index]selected = true; //保持选中状态

6、删除select

var obj = documentgetElementById("mySelect");

objparentNoderemoveChild(obj); //移除当前对象

7、select选择的响应事件

$("#mySelect")change(function(){

//添加所需要执行的 *** 作代码

})

在给标签绑定事件时在回调事件里有一个event参数,可以通过eventtarget获取当前对象,在处理函数里把当前对象当做参数传递过去。如:

//绑定事件

$('list')click(function(event){

var ele=eventtarget;

deal(ele);

});

//处理函数

function deal(obj){

}只是举一个例子,如果有错误,请指出。

public static void main(String[] args)throws Exception {

SecureRandom secureRandom1 = SecureRandomgetInstance("SHA1PRNG");

SecureRandom secureRandom2 = SecureRandomgetInstance("SHA1PRNG");

Systemoutprintln(secureRandom2equals(secureRandom1));

byte[] seed={12,11,123};

secureRandom1setSeed(seed);

secureRandom2setSeed(seed);

Systemoutprintln(secureRandom1nextInt());

Systemoutprintln(secureRandom2nextInt());

}

每次都新生成一个SecureRandom 对象,而SecureRandom 没有覆盖equals方法

所以它用超类Ojbect的equals方法

public boolean equals(Object obj) {

return (this == obj);

}

所以 Systemoutprintln(secureRandom2equals(secureRandom1));

的结果是false

但是两个对象的内部结构是一样的,所以它们设置相同的seed,执行相同方法的结果是一样的。可以查看jdk源码

以上就是关于js 获取select对象全部的内容,包括:js 获取select对象、js中如何通过单击事件获取当前对象,并传递、如何在js中获取SecureRandom对象等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9270343.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存