• jquery取radio单选按钮


            // var strMess = '<%=Exchange() %>';
    //            if (strMess == "兑换成功") {
    //                $("#btnSure").show(); //显示提交按钮
    //            }
    //            else if (strMess.length > 0) {
    //                alert(strMess); return false;
    //            }

    jquery取radio单选按钮的值
    $("input[name='items']:checked").val();
    jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关 
    获取一组radio被选中项的值 
    var item = $('input[name=items][checked]').val(); 
    获取select被选中项的文本 
    var item = $("select[name=items] option[selected]").text(); 
    select下拉框的第二个元素为当前选中值
    $('#select_id')[0].selectedIndex = 1; 
    radio单选组的第二个元素为当前选中值 
    $('input[name=items]').get(1).checked = true;

    获取值:

    文本框,文本区域:$("#txt").attr("value"); 
    多选框checkbox:$("#checkbox_id").attr("value"); 
    单选组radio: $("input[type=radio][checked]").val(); 
    下拉框select: $('#sel').val();

    控制表单元素: 
    文本框,文本区域:$("#txt").attr("value",'');//清空内容 
    $("#txt").attr("value",'11');//填充内容

    多选框checkbox: $("#chk1").attr("checked",'');//不打勾 
    $("#chk2").attr("checked",true);//打勾 
    if($("#chk1").attr('checked')==undefined) //判断是否已经打勾

    单选组radio: $("input[type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项 
    下拉框select: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项 
    $("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//添加下拉框的option 
    $("#sel").empty();//清空下拉框

    刚开始接触jquery,很多东西不熟悉 
    在用$("#id")来获得页面的input元素的时候,发现$("#id").value不能取到值

    <script type="text/javascript">
        function add()
        {    
                if ($("#TextBox1").val() == "")
                {
                    alert("输入的名称不能为空!");
                    $("#TextBox1").focus();
                    return false;
                }
                if ($("#TextBox2").val() == "")
                {
                    alert("年龄不能为空!");
                    $("#TextBox2").focus();
                    return false;
                }
                if ($("#TextBox2").val() != "")     //年龄       
                {
                    var intvar =/^d+$/;
                    if (!intvar.test($("#TextBox2").val()))
                    {
                        alert("年龄格式不正确,请输入2位数字!");
                        $("#TextBox2").focus();
                         return false;
                    }
                }
                if ($("#TextBox3").val() == "")
                {
                    alert("毕业学校不能为空!");
                    $("#TextBox3").focus();
                    return false;
                }
                if ($("#TextBoxjy").val() == "") //工作经验
                {
                    alert("工作经验不能为空!");
                    $("#TextBoxjy").focus();
                    return false;
                }
                if ($("#TextBoxjy").val() != "")     //工作经验       
                {
                    var intvar2 = /^d+$/;
                    if (!intvar2.test($("#TextBoxjy").val())) {
                        alert("格式不正确,请输入2位数字!");
                        $("#TextBoxjy").focus();
                        return false;
                    }
                }
                var sex = $("input[name='sex']:checked").val();
                var job = $("input[name='job']:checked").val();
                if (sex == undefined)
                {
                    alert("没有选择性别!");
                    return false;
                }
                if (job == undefined)
                {
                    alert("全/兼职没选中");
                    return false;
                }  
                if ($("#TextBox5").val() == "") //电话
                {
                    alert("电话不能为空!");
                    $("#TextBox5").focus();
                    return false;
                }
                if ($("#TextBox5").val() != "") {
                    var isMobile = /^(?:13d|15d)d{5}(d{3}|*{3})$/;
                    var isPhone = /^((0d{2,3})-)?(d{7,8})(-(d{3,}))?$/;
                    if (!isMobile.test($("#TextBox5").val()) && !isPhone.test($("#TextBox5").val()))
                    {
                        alert("请正确填写手机号或电话号格式不正确");
                        $("#TextBox5").focus();
                        return false;
                    }
                }
                if ($("#FreeTextBox1").val()== "")
                 {
                    alert("工作经验不能为空");
                    $("#FreeTextBox1").focus();
                    return false;
                }
                var TextBox1 = escape($("#TextBox1").val());
                var TextBox2 = escape($("#TextBox2").val());
                var TextBox3 = escape($("#TextBox3").val());
                var TextBox5 = escape($("#TextBox5").val());    
                var rad1 =escape(sex);
                var rad2 = escape(job);       
                var TextBoxjy =escape($("#TextBoxjy").val());
                var FreeTextBox1=escape($("#FreeTextBox1").val());
                var Label2 = escape($("#Label2").text());
                var Label4 = escape($("#Label4").text());     
                  $.ajax({
                    type: "POST",
                    url: "add.aspx",          
                    data:"Label2="+Label2+"&Label4="+Label4+"&TextBox1=" + TextBox1 + "&TextBox2=" + TextBox2 + "&TextBox3=" + TextBox3+"&TextBox5="+TextBox5+"&rad1="+rad1+"&rad2="+rad2+"&TextBoxjy="+TextBoxjy+"&FreeTextBox1="+FreeTextBox1,
                    success: function(msg)
                    {
                        if (msg == "ok")
                        {
                            alert("数据提交成功!");
                            window.location.href = 'JobList.aspx';
                        }
                        else
                        {
                            alert("数据提交失败!");
                        }                 
                    }
                });
                     
        }
    </script>

                           <asp:Label ID="Label1" runat="server"></asp:Label>                                  
                                  <asp:Label ID="msg" runat="server" Visible="false"></asp:Label>                                    
                                        <asp:Label ID="Label2" runat="server"></asp:Label>
                                                                   <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
                                                                                    
                           
                                        <input  type="Radio" name="job" value="全职" id="RadioButton3" runat="server"/>   
                                        <input  type="Radio" name="job" value="兼职" id="RadioButton4" runat="server"/>                                       
                                          =                                                        <input id="Submit1" type="button" value="提交简历" onclick="return add()" style="height:35px"/>
                                     
                                
               


               


     

    后来终于在伟大的百度帮助下,找到了问题的原因: 
    $("")是一个jquery对象,而不是一个dom element

    value是dom element的属性

    jquery与之对应的是val

    val() :获得第一个匹配元素的当前值。

    val(val):设置每一个匹配元素的值。

    所以,代码应该这样写:

    取值:val = $("#id")[0].value; 
    赋值: 
    $("#id")[0].value = "new value"; 
    或者$("#id").val("new value");

    或者这样也可以:val = $("#id").attr("value");

      function add() 
        {    
            var Label2 = $("#Label2").text();
            var Label4 = $("#Label4").text();
            alert(Label2);
            if (Label2 == "" && Label4 == "") 
            {
                if ($("#TextBox1").val() == "") 
                {
                    alert("输入的名称不能为空!");
                    $("#TextBox1").focus();
                    return false;
                }
                if ($("#TextBox2").val() == "") 
                {
                    alert("年龄不能为空!");
                    $("#TextBox2").focus();
                    return false;
                }
                if ($("#TextBox2").val() != "")     //年龄        
                {
                    var intvar =/^d+$/;
                    if (!intvar.test($("#TextBox2").val())) 
                    {
                        alert("年龄格式不正确,请输入2位数字!");
                        $("#TextBox2").focus();
                         return false;
                    }
                }
                if ($("#TextBox3").val() == "") 
                {
                    alert("毕业学校不能为空!");
                    $("#TextBox3").focus();
                    return false;
                }
                if ($("#TextBoxjy").val() == "") //工作经验 
                {
                    alert("工作经验不能为空!");
                    $("#TextBoxjy").focus();
                    return false;
                }
                if ($("#TextBoxjy").val() != "")     //工作经验        
                {
                    var intvar2 = /^d+$/;
                    if (!intvar2.test($("#TextBoxjy").val())) {
                        alert("格式不正确,请输入2位数字!");
                        $("#TextBoxjy").focus();
                        return false;
                    }
                }
    //            var RadioButton1 = $('input:radio[name="RadioButton1"]:checked').val(); //性别
    //            var RadioButton2 = $('input:radio[name="RadioButton2"]:checked').val(); //性别
    //            if (RadioButton1 == null || RadioButton2==null) 
    //            {
    //                alert("性别没选中!");
    //               // $("#TextBoxjy").focus();
    //                return false;
    //            }
    //            var RadioButton3 = $('input:radio[name="RadioButton3"]:checked').val(); //性别
    //            var RadioButton4 = $('input:radio[name="RadioButton4"]:checked').val(); //性别
    //            if (RadioButton3 == null || RadioButton4==null)
    //             {
    //                alert("全/兼职没选中!");
    //                // $("#TextBoxjy").focus();
    //                return false;
    //            }  
    //            if ($("#TextBox5").val() == "") //电话 
    //            {
    //                alert("电话不能为空!");
    //                $("#TextBox5").focus();
    //                return false;
    //            }
    //            if ($("#TextBox5").val() != "") {
    //                var isMobile = /^(?:13d|15d)d{5}(d{3}|*{3})$/;
    //                var isPhone = /^((0d{2,3})-)?(d{7,8})(-(d{3,}))?$/;
    //                if (!isMobile.test($("#TextBox5").val()) && !isPhone.test($("#TextBox5").val())) 
    //                {
    //                    alert("请正确填写手机号或电话号格式不正确");
    //                    $("#TextBox5").focus();
    //                    return false;
    //                }
    //            }
    //            if ($("#FreeTextBox1").val() != "")
    //             {
    //                alert("工作经验不能为空");
    //                $("#FreeTextBox1").focus();
    //                return false;
    //            }
    //            var TextBox1 = escape($("#TextBox1").val());
    //            var TextBox2 = escape($("#TextBox2").val());
    //            var TextBox3 = escape($("#TextBox3").val());
    //            var TextBox5 = escape($("#TextBox5").val());     
    //            var rad1 =escape(RadioButton1);
    //            var rad2 =escape(RadioButton2);
    //            var rad3 =escape(RadioButton3);
    //            var rad4 =escape(RadioButton4);
    //            var TextBoxjy =escape($("#TextBoxjy").val());
    //            var FreeTextBox1=escape($("#FreeTextBox1").val()); 
    //            var Label2 = escape($("#Label2").text());
    //            var Label4 = escape($("#Label4").text());
    //            if(rad1!=null&&rad3!=null)
    //            {       
    //                $.ajax({
    //                    type: "POST",
    //                    url: "Control/WebUserControl.ascx",
    //                    data:"Label2="+Label2+"&Label4="+Label4+"&TextBox1=" + TextBox1 + "&TextBox2=" + TextBox2 + "&TextBox3=" + TextBox3+"&TextBox5="+TextBox5+"&rad1="+rad1+"&rad3="+rad3+"&TextBoxjy="+TextBoxjy+"&FreeTextBox1="+FreeTextBox1+",
    //                    success: function(msg)
    //                    {
    //                        if (msg == "cf") 
    //                        {
    //                            alert("请换个姓名!");
    //                        }
    //                        if (msg == "ok") 
    //                        {
    //                            alert("数据提交成功!");
    //                            window.location.href = 'JobList.aspx';
    //                        }
    //                        else 
    //                        {
    //                            alert("数据提交失败!");
    //                        }                    
    //                    }
    //                });
    //           }
    //       
                  
        }
    </script>

  • 相关阅读:
    初学Python3
    性能测试学习成长图
    k8s集群部署mysql(docker自创建镜像)
    docker 部署uwgsi+python 启动报错 Python 3 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)
    jenkins 打安卓包 cpu使用过高处理操作
    docker部署mysql,nginx,php,并上传镜像到私有仓库
    Linux下PHP7.2扩展
    docker部署Eurake服务,服务节点无法注册服务
    本地Pycharm将spark程序发送到远端spark集群进行处理
    spark集群安装并集成到hadoop集群
  • 原文地址:https://www.cnblogs.com/dqh123/p/9469984.html
Copyright © 2020-2023  润新知