jquery对表单元素的一些操作

来源:互联网 发布:小米电视有线网络ip 编辑:程序博客网 时间:2024/05/21 11:30

jQuery对表单元素的取值,赋值

//文本框   文本域    $("#text_id").val();//取值  $("#text_id").attr("value",'test');//赋值    //单选按钮    $("input[type=radio]:checked").val(); //取值  //赋值  $('input[name=items]').get(1).checked = true;//radio单选组的第二个元素为当前选中值  $("input[type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项  $("input[name='radioName'][value=2]").attr("checked",true); //设置value=2的项目为当前选中项  var boolCheck=$('input:radio[name="sex"]').is(":checked");//判断单选按钮是否选中    //复选框  $("#checkbox_id").attr("value");//取值  $("#chk_id").attr("checked",true);//勾选  $("#chk_id").attr("checked",");//未勾选  if($("#chk_id").attr('checked')==true) //判断是否已经选中        //下拉菜单  $('#select_id').val();//取值  $("select[name=items] option:selected").text(); //获取select被选中项的文本  $("#select_id").attr("value",'test');//设置value=test的项目为当前选中项   $("testtest2").appendTo("#select_id")//添加下拉框的option   $("#select_id").empty();//清空下拉框  $('#select_id')[0].selectedIndex = 1; //select下拉框的第二个元素为当前选中值  

input输入框取值的方法

<script type="text/javascript">        //使用id的方式获取        $(document).ready(function(){            //1            $("#button_text1").click(function(){                var result1 = $("#input_text1").val();                alert("result1 = " + result1);            });            //2            $("#button_text2").click(function(){                var result2 = $("input[id='input_text2']").val();                alert("result2 = " + result2);            });            //3            $("#button_text3").click(function(){                var result3 = $("input[id='input_text3']").attr("value");                alert("result3 = " + result3);            });            //4. 可以通过type的值来获取input中的值(未演示)            /*            $("#button_text4").click(function(){                var result4 = $("input[type='text']").val();                alert("result4 = " + result4);            });            */            //5. 可以通过name的值来获取input中的值(未演示)            /*            $("#button_text5").click(function(){                var result5 = $("input[name='text']").val();                alert("result5 = " + result5);            });                        */        });        </script>


几个地方转过来的

0 0
原创粉丝点击