zoukankan      html  css  js  c++  java
  • jquery 元素文本取值/赋值

    select元素选中option值

    jq

    $("#select1  option:selected").text();
    
    $("select id或class  option:selected").text();

    js获取select标签选中的值

    var obj = document.getElementByIdx_x(”testSelect”); //定位id
    
    var index = obj.selectedIndex; // 选中索引
    
    var text = obj.options[index].text; // 选中文本
    
    var value = obj.options[index].value; // 选中值

    单选按钮(Radio)当前选中项的值

    jq

    var value  = $('input[name="radioName"]:checked').val(); //获取被选中Radio的Value值

    js

    <input type="radio" name="DoorCt" value="twoDoor" checked=”true”onclick="getValue()">Two  
    <input type="radio" name="DoorCt" value="fourDoor" onclick="getValue()">Four

    1 按照name属性获取该radio的集合

    2 遍历集合中的每一项元素

    3 获取元素的checked属性,是否为true,为true,返回其value值

    function getValue(){  
        // method 1   
        var radio = document.getElementsByName("DoorCt");  
        for (i=0; i<radio.length; i++) {  
            if (radio[i].checked) {  
                alert(radio[i].value)  
            }  
        }  
    }

    这里使用alert(radio[i].value)是为了直观的效果,可以使用return radio[i].value 来返回value值。

    细看一下getValue方法的实现。

    var document.getElementsByName("DoorCt");

    根据name属性,获取radio的集合,getElementsByName方法是document获取对象的三个方法之一,获取到的是集合。

    紧接着 for (i=0;i<radio.length; i++) { 对集合中的元素开始遍历

    if (radio[i].checked) { 遍历每一个元素时,检查一下元素的checked属性,是否为true,为true的,则是被选中的,将其值radio[i].value 值返回。

  • 相关阅读:
    Bundle类
    AlertDialog
    认识Android
    TextView属性详解
    Android中设置文字大小的定义类型
    理解偏差
    python爬虫实验2
    python爬虫实验
    PHP sql注入漏洞修复(字符串型)
    java实现远程控制
  • 原文地址:https://www.cnblogs.com/chenglj/p/7485918.html
Copyright © 2011-2022 走看看