js to obtain the drop-down list to select the value and text (select), as well as access to radio buttons (radio) group and modify the selected value of

First, access the drop-down list to select the value and text (select)

<html>
<head><title>Gets the drop-down list to select key values, and text (select)</title></head>
<body>
<script>

//Gets the drop-down list to select key text
function getSelectedText(name){
var obj=document.getElementById(name);
for(i=0;i<obj.length;i++){
   if(obj[i].selected==true){
    return obj[i].innerText;      //The key is by option object the InnerText property gets the option text
   }
}
}

//Gets the drop-down list to select key value
function getSelectedValue(name){
var obj=document.getElementById(name);
return obj.value;      //So simple, the direct use of the object's value property to get to the
}

</script>
<select>
<option value="fist">1</option>
<option value="second">2</option>
<option value="third">3</option>
</select>

<input type="button"   value="The selected text "   />
<input type="button"   value="The selected value " />
</body>
</html>

Second, access to radio buttons (radio) group and modify the selected value of

To see a lot of posts said js access the radio button (radio) group can be directly used the value of document.getElementById ( "oper"). Value, although the radio button group with the same drop-down list (list item is an array) is also an array in this way can be of value to the drop-down list, but the radio button group that they were less than the selected value. Under carefully studied, summarized as follows:

With different drop-down list, radio button or this.form.oper have to use document.getElementsByName ( 'oper') access to the array object, document.getElementById ( 'oper') Can not access to the array object (select can). But also to obtain the value of one cycle have to judge through acquisition, should not directly. Value (select can). And to change the radio button group to select items, but also have to change with each cycle of the radio button to determine the value.

Radio.html test code is as follows:

<html>
<head></head>
<script language="javascript">

//For the radio button's value for a single option and more options. Not selected return false; selecting the item and returns the value of an option.
function getRadio(oRadio){
var oRadioLength= oRadio.length;
var oRadioValue = false;
//alert("oRadioLength:["+oRadioLength+"]");

if (oRadioLength== undefined){
      if (oRadio.checked){
       oRadioValue = oRadio.value;
      }
}else{
      for (i=0;i<oRadioLength;i++){
       //alert("oRadio["+i+"]:"+oRadio[i].checked+"/"+oRadio[i].value);
       if (oRadio[i].checked){
        oRadioValue = oRadio[i].value;
        break;
       }
      }
}
return oRadioValue;
}

//Improvement: 

//For the radio button's value, pass the name as a parameter to the radio. Not selected return false; selecting the item and returns the value of an option.
function getRadioValue(name){
var radioes = document.getElementsByName(name);
for(var i=0;i<radioes.length;i++)
{
     if(radioes[i].checked){
      return radioes[i].value;
     }
}
return false;
}

//By value, modify the selected radio button
function changeRadio(oRadio,oRadioValue){        //Passing in an object
for(var i=0;i<oRadio.length;i++) //Loop
{
        if(oRadio[i].value==oRadioValue) //To compare values
        {
         oRadio[i].checked=true; //Modify the selected
         break; //Stop the loop
        }
}
}

//Improvement: 

//By value, modify the selected radio button
function setRadio(name,sRadioValue){        //Incoming radio of name and value of the selected item
var oRadio = document.getElementsByName(name);
for(var i=0;i<oRadio.length;i++) //Loop
{
        if(oRadio[i].value==sRadioValue) //To compare values
        {
         oRadio[i].checked=true; //Modify the selected
         break; //Stop the loop
        }
}
}

</script>

<body>
<form name="frm">

<input type="radio" name="oper" value="agree"        >Agree with </td>
<input type="radio" name="oper" value="downchange" checked>Under modify </td>
<input type="radio" name="oper" value="refuse">Declined </td>
<input type="radio" name="oper" value="report" >Escalation </td>
<br>
alert('result:'+getRadio(this.form.oper))
<input type="button" name="test1" value="Button 1 ">
<br>
alert('result:'+getRadio(document.getElementById('oper')))
<input type="button" name="test2" value="Button 2 ">
<br>
alert(this.form.oper.value)
<input type="button" name="test3" value="Button 3 ">
<br>
changeRadio(this.form.oper,"Escalation ")
<input type="button" name="test4" value="Button 4 ">
<br><br><br><br>    

<select>
<option value="agree" >Agree with </option>
<option        value="downchange" selected>Under modify </option>
<option value="refuse">Declined </option>
<option        value="report">Escalation </option>
</select>
<br>
alert(this.form.slt.value)
<input type="button" name="test5" value="Button 5 ">
<br>
document.getElementById('slt')[2].innerText)
<input type="button" name="test6" value="Button 6 ""alert(document.getElementById('slt')[2].innerText);">

</form>
</body>
</html>

Reprint Address: http://hi.baidu.com/nirvanan/blog/item/5b841539aa872af5b311c7c2.html

分类:AJAX 时间:2009-03-07 人气:1674
分享到:
blog comments powered by Disqus

相关文章

  • Java calls. Net provides no reference webservice return type is an array 2010-10-08

    Recently, the company is a project with a Java call to each other. Net provided by the webservice, the method return type is an array with no parameters, debugging code that is abnormal, ask you senior wing, very grateful! My code is as follows: Stri

  • oracle custom functions return a table type (transfer) 2011-03-08

    The function can return oracle table type, but this is actually a collection of table type type (and similar to an array) This type can not be directly from the object as a Oracle 9i from the start, providing a called "pipelined table function"

  • PHP JSON出错:Cannot use object of type stdClass as array解决方法 2014-04-18

    这篇文章主要介绍了PHP JSON出错:Cannot use object of type stdClass as array解决方法,需要的朋友可以参考下 php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误: Cannot use object of type stdClass as array 产生原因: 复制代码 代码如下: $res = json_decode($res); $res['key']; //把 json_decode

  • js修改input的type属性问题探讨 2014-08-08

    当input元素还未插入文档流之前,是可以修改它的值的,在ie和ff下都没问题.但如果input已经存在于页面,其type属性在ie下就成了只读属性了,不可以修改 js修改input的type属性有些限制.当input元素还未插入文档流之前,是可以修改它的值的,在ie和ff下都没问题.但如果input已经存在于页面,其type属性在ie下就成了只读属性了,不可以修改.在ff下仍是可读写属性. 今天遇到个问题,输入框有默认值"密码",但获得焦点时,"密码"两字会去掉,

  • js修改input的type属性及浏览器兼容问题探讨与解决 2015-01-07

    js修改input的type属性有些限制,今天遇到个问题一开始的时候,input的类型是text,后来变成了password类型.直观的思路是用js修改input的type类型.但ie下这么做不可行,所以只能换个思路感兴趣的朋友可以了解下 js修改input的type属性有些限制.当input元素还未插入文档流之前,是可以修改它的值的,在ie和ff下都没问题.但如果input已经存在于页面,其type属性在ie下就成了只读属性了,不可以修改.在ff下仍是可读写属性. 今天遇到个问题,输入框有默认

  • hibernate query data table only to return a char type field characters 2011-05-19

    hibernate query data table only to return a char type field in oracle or sqlServer next character encountered, the query data table field of type char, if you use Hibernate to execute native SQL queries, often only to return A character. Because of c

  • PHP错误Cannot use object of type stdClass as array in错误的解决办法 2013-11-13

    很多人在PHP输出一个二维数组的时候出现"Fatal error: Cannot use object of type stdClass as array in--".这篇文章主要介绍了该错误的解决办法,需要的朋友可以参考下 很多人在PHP输出一个二维数组的时候出现"Fatal error: Cannot use object of type stdClass as array in--". 解决办法分析如下: 现有这样一个二维数组: Array ( [0] =&g

  • ie8下修改input的type属性报错的解决方法 2014-07-31

    当用户勾选显示明文复选框时,要以明文显示用户输入的密码,去掉勾选时要变回密文,问题是ie8中是不允许修改input的type属性 摘要: 现在有一个需求如图所示,当用户勾选显示明文复选框时,要以明文显示用户输入的密码,去掉勾选时要变回密文,刚开始想到的就是修改输入框的type来决定显示明文还是密文,使用jQuery的attr来做试验,测试结果是chrome,Firefox,ie9+都是好的,在ie8以下就会报错,查找了下原因,ie8中是不允许修改input的type属性,最终换了种思路实现. 当

  • js清除input中type等于file的值域(示例代码) 2015-03-14

    本篇文章主要介绍了js清除input中type等于file的值域(示例代码) 需要的朋友可以过来参考下,希望对大家有所帮助 如下所示;var objFile = document.getElementById('fileID'); objFile.outerHTML=objFile.outerHTML.replace(/(value=\").+\"/i,"$1\"");

  • Background of the returned result set is object type, not the specific object? 2010-09-14

    Background of the returned result set is object type, not the specific object? Solution: you must create a corresponding object and background dto the vo binding; in the program statement variables such as: a; xxxVo (which I do not understand why it

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.502 (s). 12 q(s)