JS操作SELECT表单大全,赋默认值,取值,增,删等

经常用到的代码,却总是忘掉。总结一下,放在这里。
在下面所有代码前最好加上这句:


var selectId=document.getElemengById('selectId');

清空select的项


selectId.options.length = 0;

如果留下第一行的话就是


selectId.options.length = 1;

向select选项中 加入一个Option


var varOption = new Option(objOptionText,objOptionValue);
selectId.options[selectId.options.length] = varOption;
//或selectId.options.add(varOption);


从select选项中 删除一个Option


 for(var i=0;i<selectId.options.length;i++)
 {
     if(selectId.options[i].value == objOptionValue)
     {
         selectId.options.remove(i);
         break;
     }
 }      

设置select中text=”paraText”的第一个Option为选中


     for(var i=0;i<selectId.options.length;i++)
     {
         if(selectId.options[i].text == objOptionText)
         {
             selectId.options[i].selected = true;
             isExit = true;
             break;
         }
     }    

设置select中value=”paraValue”的Option为选中


selectId.value = objOptionValue;

得到select的当前选中项的value


var currSelectValue = selectId.value;

得到select的当前选中项的text


var currSelectText = selectId.options[selectId.selectedIndex].text;

得到select的当前选中项的Index


var currSelectIndex = selectId.selectedIndex;


( ! ) Warning: Missing argument 1 for cwppos_show_review(), called in /data/www/enjoyphp/wp-content/themes/flat/content-single.php on line 29 and defined in /data/www/enjoyphp/wp-content/plugins/wp-product-review/includes/legacy.php on line 18
Call Stack
#TimeMemoryFunctionLocation
10.0000354824{main}( ).../index.php:0
20.0001355112require( '/data/www/enjoyphp/wp-blog-header.php' ).../index.php:17
30.172310160520require_once( '/data/www/enjoyphp/wp-includes/template-loader.php' ).../wp-blog-header.php:19
40.173610231664include( '/data/www/enjoyphp/wp-content/themes/flat/single.php' ).../template-loader.php:74
50.260210793640get_template_part( ).../single.php:5
60.260210794160locate_template( ).../general-template.php:167
70.260210794256load_template( ).../template.php:643
80.260210794640require( '/data/www/enjoyphp/wp-content/themes/flat/content-single.php' ).../template.php:686
90.266210779736cwppos_show_review( ).../content-single.php:29

1 评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注