radio,checkbox,select,input text获取值,设置哪个默认选中

11

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>测试表单获取元素值</title>
    <link rel="stylesheet" type="text/css" href="../bootstrap-3.3.7-dist/css/bootstrap.min.css">
    <script type="text/javascript" src="../js/jquery-3.2.1.min.js"></script>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        .main{
            padding: 0px 15px;
        }
        input{
            margin-right: 15px;
            display: inline-block;
        }
    </style>
</head>
<body>
    <div class="main">
        <div class="row">
            <label>姓名:</label><input type="text" name="name" value="" />
        </div>
        <div class="row">
            <label>多选:</label>
            <label for="checkbox1">多选1:</label><input type="checkbox" name="多选" id="checkbox1" value="checkbox1">
            <label for="checkbox2">多选2:</label><input type="checkbox" name="多选" id="checkbox2" value="checkbox2">
            <label for="checkbox3">多选3:</label><input type="checkbox" name="多选" id="checkbox3" value="checkbox3">
        </div>
        <div class="row">
            <label>下拉:</label>
            <select name="opt">
                <option value="opt1" name="opt">下拉1</option>
                <option value="opt2" name="opt">下拉2</option>
                <option value="opt3" name="opt">下拉3</option>
                <option value="opt4" name="opt">下拉4</option>
            </select>
        </div>
        <div class="row">
            <label>单选:</label>
            <label for="radio1">单选1:</label><input type="radio" name="radio" id="radio1" value="radio1">
            <label for="radio2">单选2:</label><input type="radio" name="radio" id="radio2" value="radio2">
            <label for="radio3">单选3:</label><input type="radio" name="radio" id="radio3" value="radio3">
            <label for="radio4">单选4:</label><input type="radio" name="radio" id="radio4" value="radio4">
        </div>
        <button id="submit">提交</button>
    </div>
    <script type="text/javascript">
        $("#submit").on("click",function(){
            //给多选框的某个选中
            $(".main").find("input[name=‘多选‘]")[1].checked=true;
            //获取多选框选中的索引:
            console.log("多选框suoyin:"+$("input[name=‘多选‘]").index($("input[name=‘多选‘]:checked")));
            //获取复选框选中的值:
            var arr=document.getElementsByName("多选");
            for(var i=0;i<arr.length;i++){
                if(arr[i].checked){
                    console.info("多选框:"+arr[i].value);
                }
            }
             //反选
            // $(‘input[name="多选"]‘).each(function () {
            //     $(this).prop("checked", !$(this).prop("checked"));
            // });

            //输入框设置值
            $("input[name=‘name‘]").val("wlz111")
            //获取输入框值:
            console.info("输入框:"+$("input[name=‘name‘]").val());

            //将下拉框设置成某个值
            $(".main").find("select[name=‘opt‘]").get(0).selectedIndex = 3;
            //获取下拉框值
            console.log("下拉框:"+$(‘select‘).val());

            //单选按钮设置值
            $(".main").find("input[name=‘radio‘]")[3].checked=true;
            //获取单选按钮的值:
            console.log("单选按钮:"+$(".main").find("input[name=‘radio‘]:checked").val());
            //获取单选按钮选中的索引:
            console.log("单选按钮suoyin:"+$("input[type=radio]").index($("input[type=radio]:checked")));  

        })
    </script>
</body>
</html>

原文地址:https://www.cnblogs.com/wulinzi/p/8350655.html

时间: 2024-08-29 09:50:40

radio,checkbox,select,input text获取值,设置哪个默认选中的相关文章

JSP的3种方式实现radio ,checkBox,select的默认选择值

JSP的3种方式实现radio ,checkBox,select的默认选择值.以radiao 为例:第一种方式:在jsp中使用java 脚本,这个方法最直接,不过脚本太多,不容易维护<%String state = request.getParrameter("state" )%> <td width="27"><input type="radio" name="state" value=&quo

Radio Checkbox Select 操作

一个小总结 <!DOCTYPE html> <html> <head> <meta name="description" content="[radio and checkbox]"> <meta charset="utf-8"> <title>JS Bin</title> <script src="https://code.jquery.com/

jquery操作select详解(取值,设置选中)

每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select> 1.设置value为pxx的项选中 $(".selector").val("pxx"); 2.设置text为pxx的项选中 $(".selector").find("option[text='pxx']").attr(&qu

v-model 结合 radio checkbox select的使用

*v-model其实是一个语法糖,它的背后本质上是包含两个指令操作: 1.v-bind绑定一个value属性2.v-on指令给当前元素绑定input事件* v-model结合单选框使用 <label for="male"> <input type="radio" name="sex" id="male" value="男" v-model="sex" />男 &l

Dynamics 365 获取值 设置值

//获取new_name的值(单行文本)Xrm.Page.getAttribute("new_name").getValue()//设置new_name的值(单行文本)Xrm.Page.getAttribute("new_name").setValue("1121")//获取客户集合(查找)Xrm.Page.getAttribute("new_accounid").getValue()//设置客户值(查找)Xrm.Page.g

JQuery实现页面所有input text的值相加 显示到汇总

$(function () { var data = {}; $("[id^=GROSSAREA]").each(function (index, obj) { $(obj).bind("blur", function () { data[index] = $(this).val(); showResult(); }).bind("keyup", function () { var v = $(this).val(); $(this).val(v

jQuery的属性操作/使用jQuery操作input的value值

一.jQuery的属性操作 jQuery的属性操作模块分成四个部分: HTML属性操作, DOM属性操作, 类样式操作和值操作 HTML属性操作: 是对HTML文档中的属性读取,设置和移除操作; 比如:attr(), removeAttr() DOM属性操作: 对DOM元素的属性进行读取, 设置和移除操作; 比如prop(), removeProp() 类样式操作: 是指对DOM属性className进行添加, 移除操作; 比如addClass(), removeClass(), toggleC

:radio :checkbox

匹配所有单选按钮 示例 描述: 查找所有单选按钮 HTML 代码: <form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input ty

asp.mvc获取checkbox、radio、select的值

记录一下在asp.mvc中,提交表单时后台获取checkbox.radio.select值的方法. 1.获取select的值 <select name="type"> <option value="Phone">手机</option> <option value="Pad">平板</option> <option value="Pc">电脑</opt