- 浏览: 1734778 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (337)
- javaScript校验 (11)
- java (31)
- java连接数据库 (1)
- js应用 (41)
- JQuery (15)
- linux操作命令 (12)
- loadrunner测试 (5)
- tomcat (18)
- 数据库 (12)
- eclipse (7)
- 触发器 (7)
- 表单 (3)
- ibatis (6)
- oracle (4)
- xml (1)
- Exception (6)
- spring (16)
- struts (4)
- struts 标签 (2)
- sql (8)
- sql server (6)
- 其它 (18)
- Apache (2)
- 电脑故障 (4)
- java 线程 (1)
- dwr (8)
- jackey (18)
- 总结 (34)
- gcc linux (2)
- extjs 学习 (5)
- 网站建设 (4)
- 健康 (2)
- 房地产知识 (1)
- hibernate (3)
- mysql (7)
- linux (13)
- svn (1)
最新评论
-
阳光泛滥的日子:
很好用谢谢
java.net.URISyntaxException的解决办法 -
linjianqing:
现在有更好的处理方式吗?我正也为这发愁
applet访问打印机出现的问题 -
ruyi574812039:
非常感谢您的总结!想问几个问题!假设三个项目分别以静态部署的三 ...
在Tomcat中部署Web程序的几种方式 -
yangguo:
太j8麻烦了
Spring3.1中使用缓存注解及Shiro的缓存联合 -
bellawang:
很好用谢谢
java.net.URISyntaxException的解决办法
This is an option box:
<form name="testform"> <select name="testselect"> <option value="first">first option</option> <option value="second">second option</option> <option value="third">third option</option> <option>your browser can't handle this script</option> </select> </form>
To access options in a select box, you use the following code:
document.forms['testform'].testselect.options[i]
where i
is the number of the option you want to access. Remember that the first option is options[0]
, the second one is options[1]
etc.
Now if you want to delete the option, do
document.forms['testform'].testselect.options[i] = null;
By making it null
, the option is completely removed from the list. Note that this also affects the numbering of the options. Suppose you remove option[1]
from the example above, the old option[2]
('Third option') becomes option[1]
etc.
To create a new option, do:
document.forms['testform'].testselect.options[i] = new Option('new text','new value');
where new text is the text the user sees and new value is the VALUE of the new option, the bit that's sent to the server when the form is submitted. Of course the new option overwrites the old one.
To completely clear a select box, do
document.forms['testform'].testselect.options.length = 0;
Object detection
<OPTION>Your browser can't handle this script</OPTION>
As to this cryptic option in the example above, it is for object detection.
This is more difficult than usual because the option object itself is supported by all browsers. What we want to know here is if we can add or remove option objects. The trick is to put one extra option in one select box. A script called onLoad
tries to remove this option. If, after that, the option's still there the browser has failed its support detect.
This script performs the detect and gives a variable optionTest
. If it's false the browser can't handle dynamic options and you should not execute the scripts.
function init() { optionTest = true; lgth = document.forms['testform'].testselect.options.length - 1; document.forms['testform'].testselect.options[lgth] = null; if (document.forms['testform'].testselect.options[lgth]) optionTest = false; } <BODY onLoad="init()">
If you select the place of the test option wisely, the text Your browser can't handle this script informs people with old browsers that they can't use this select box script, while users with modern browsers never see the option since it's removed quickly.
You can also place one in the option if you don't want any text.
Example
Try the example below. The options in the second box are links.html css javascript
EvoltCSS1 Mastergrid
One of the strange things I discovered is that the target select box (the second one) must have a SIZE of more than 1, or else Netscape and Explorer each develop their own bugs.
The script
var store = new Array(); store[0] = new Array( 'HTML Compendium', 'http://www.htmlcompendium.org', 'Web Designer\'s Forum', 'http://www.wdf.net'); store[1] = new Array( 'Web Designer\'s Forum', 'http://www.wdf.net', 'CSS1 Mastergrid', 'http://webreview.com/wr/pub/guides/style/mastergrid.html'); store[2] = new Array( 'Stefan Koch\'s JavaScript Tutorial', 'http://rummelplatz.uni-mannheim.de/~skoch/js/tutorial.htm', 'Client Side JavaScript Reference', 'http://developer.netscape.com/docs/manuals/js/client/jsref/index.htm', 'Web Designer\'s Forum', 'http://www.wdf.net'); function init() { optionTest = true; lgth = document.forms[0].second.options.length - 1; document.forms[0].second.options[lgth] = null; if (document.forms[0].second.options[lgth]) optionTest = false; } function populate() { if (!optionTest) return; var box = document.forms[0].first; var number = box.options[box.selectedIndex].value; if (!number) return; var list = store[number]; var box2 = document.forms[0].second; box2.options.length = 0; for(i=0;i<list.length;i+=2) { box2.options[i/2] = new Option(list[i],list[i+1]); } }
init()
is called onLoad, as explained above.
This select box triggers the changing of the other one:
<select size=4 name="first" width=200 onchange="populate()"> <option value="0">html</option> <option value="1">css</option> <option value="2">javascript</option> </select>
Explanation
First of all, create some arrays that hold the options. Options are stored in name/value pairs.
var store = new Array(); store[0] = new Array( 'HTML Compendium','http://www.htmlcompendium.org', etc.
Please note that the number of the array corresponds with the value of the option: HTML has value 0 and if it's selected it writes store[0]
into the second select box.
Then for the actual script. First check if optionTest
is false
, if so the script is not executed.
function populate() { if (!optionTest) return;
Get the value of the selected option. This should be a number.
var box = document.forms[0].first; var number = box.options[box.selectedIndex].value;
If for some reason it doesn't exist, end the function. This gives you the freedom to put empty options in the select boxes (like -- Select a category --).
if (!number) return;
Get the correct option list store[number]
and put it in list[]
. Then access the second select box.
var list = store[number]; var box2 = document.forms[0].second;
Clear the second select box.
box2.options.length = 0;
Finally, go through the correct list of name/value pairs and make each of them a new option in the second select box.
for(i=0;i<list.length;i+=2) { box2.options[i/2] = new Option(list[i],list[i+1]); } }
Explorer 5.0 problems
When it comes to dynamically generating options and selects, Explorer 5.0 on Windows has quite a few problems:
- Generating options in another frame or window doesn't work. Put the script in the page that contains the select. I have heard, but did not test, that this problem still exists in Explorer 6.0
- Generating selects and options through the 'pure' W3C DOM (ie. with any
document.createElement()
) crashes Explorer 5.0 . Solved in 5.5
Generate these selects and options throughinnerHTML
instead. - Generating options from a popup window may crash any Explorer Windows.
I have heard, but did not test, that the script below works fine in IE 5.0:
var doc = select.ownerDocument; if (!doc) doc = select.document; var opt = doc.createElement('OPTION'); opt.value = value; opt.text = text; select.options.add(opt, index);
发表评论
-
JS 中的 style.width
2009-09-01 17:59 3417JS 中的 style.width 2009-08-28 1 ... -
js截取固定长度的中英文字符
2008-11-26 14:16 6278js虽然提供了计算字符串所占字节数的函数,但是却不能正确计算汉 ... -
JavaScript中的escape() 函数
2008-11-26 13:51 2035<script type="text/jav ... -
js option删除代码集合
2008-11-25 13:52 2288javascript删除option选项的多种方法,方便大家选 ... -
移动(增加删除)option
2008-11-25 13:33 1129PHP代码: <!DOCTYPE html ... -
HTML中meta标签用法详解
2008-10-08 13:04 1922meta是html语言head区的一 ... -
网页中多媒体的播放代码大全
2008-10-08 12:55 1920<embed src="在此替 ... -
播放器代码大全
2008-10-08 12:54 22241.avi格式 <object id="vi ... -
DIV边框代码
2008-10-08 11:55 22014立体线框代码稍加润色后的效果: <div style ... -
div+css实现圆角边框
2008-10-08 11:53 2620div+css实现圆角边框,在网络上查看了一下,很多都是实现圆 ... -
JS常用语句
2008-10-08 11:26 14311.document.write( " " ... -
怎样使可编辑的DIV固定大小,当内容多时,就自动出现滚动条
2008-10-08 10:59 6903方法一 <style type="text/c ... -
div 让文字或图片居中
2008-10-08 10:52 4240在div 中让 文字或图片居中,请参考以下代码1: - - - ... -
setInterval("",1000) 和setTimeout(,3000);的使用
2008-10-08 10:50 3097应用代码一: <html xmlns=" ... -
js函数window.open()的使用
2008-10-08 09:22 3747在javascript中,有一个函数 window.open( ... -
40种网页常用小技巧(JavaScript)
2008-10-08 09:12 16531. oncontextmenu="wi ... -
根据判断浏览器类型屏幕分辨率自动调用不同CSS的代码
2008-10-07 18:07 2602既判断浏览器,也判断 ... -
JS 判断浏览器类型(是否使用IE,Firefox,Opera浏览器)
2008-10-07 17:59 12490关键字:JS 判断浏览器(是否使用IE,Firefox ... -
js判断浏览器类型
2008-10-07 17:56 2350function btnlogin(){ if(nav ... -
navigator.userAgent.indexOf来判断浏览器类型
2008-10-07 17:43 11925<script language="Jav ...
相关推荐
标题中提到的“js操作select和option常用代码整理”涉及到了使用JavaScript编程语言来操作HTML中的select元素和option元素的相关代码。在Web开发中,select和option元素通常用来创建下拉列表,供用户从中选择一项或...
jQuery mobile中,有input list属性,下方紧跟标签,中间包含的<option value=”XXXX”></option>即为选项,相当于这个input为有下拉列表的功能,当然,input的list名称需要和datalist中的id一致。 <input id=...
ECharts 是一个基于 JavaScript 的数据可视化库,广泛应用于网页数据图表的绘制。它提供了丰富的图表类型,如柱状图、...在提供的`demo`文件中,应该包含了完整的示例代码,你可以参考它来构建自己的全屏ECharts图表。
- 微信的android版本的内置浏览器是腾讯x5,大致相当于webkit40,具体版本号很神秘,渲染能力和性能有些问题,参考官方说明[Inspector调试WebView](http://x5.tencent.com/guide?id=2001) ## 其他注意事项: - ...
Puppeteer 是一个由 Google Chrome 团队开发的 Node.js 库,它提供了一种高级 API 来通过 DevTools 协议控制 Chrome 或 Chromium。在自动化操作 Puppeteer 中,我们可以利用其强大的功能来模拟用户交互、网页渲染、...
JavaScript采用的是Perl语言正则表达式语法的一个相当完整的子集. 正则表达式的模式规范是由一系列字符构成的.大多数字符(包括所有字母数字字符)描述的都是按照字面意思进行匹配的字符.这样说来,正则表达式/java/...
网管教程 从入门到精通软件篇 ★一。★详细的xp修复控制台命令和用法!!! 放入xp(2000)的光盘,安装时候选R,修复! Windows XP(包括 ...JS:javascript源文件 JSP:HTML网页,其中包含有对一个Java servlet...