- 来之http://blog.csdn.net/wangsen2235068/article/details/8910423
- <script src="../Scripts/jquery-1.4.1.min.js"></script>
- <script type="text/ecmascript">
- $(function () {
- //select
- $("#Button1").click(function () {
- var v = $("#selProgram").val();
- alert(v);
- });
- $("#Button2").click(function () {
- var v = $("#selProgram option:selected").text();//表单过滤器 :enabled,:disabled,:checked,:selected
- alert(v);
- });
- $("#Button3").click(function () {
- $("#selProgram option[value='3']").attr("selected", "selected");//属性过滤器,必须是dom已经有了这个属性,比如虽然select有文本值,可以通过text()方法获取到值,但是不可以通过attr("text")获取,因为没有这个属性
- });
- $("#Button4").click(function () {
- $("#selProgram option").each(function () {
- if($(this).text()=="Java")
- $(this).attr("selected", "selected");
- });
- });
- //radio
- $("#Button5").click(function () {
- var v = $(":radio[name='radProgram']:checked").val();
- alert(v);
- });
- $("#Button6").click(function () {
- var v = $(":radio[name='radProgram']:checked").next().text();//表单过滤器 :enabled,:disabled,:checked,:selected
- alert(v);
- });
- $("#Button7").click(function () {
- $(":radio[name='radProgram'][value='3']").attr("checked", "checked");//属性过滤器,必须是dom已经有了这个属性,比如虽然select有文本值,可以通过text()方法获取到值,但是不可以通过attr("text")获取,因为没有这个属性
- });
- $("#Button8").click(function () {
- $(":radio[name='radProgram']").each(function () {
- if ($(this).next().text() == "Java")
- $(this).attr("checked", "checked");
- });
- });
- //Checkbox
- $("#chkAll").click(function () {
- $("#divProgram :checkbox").attr("checked", $(this).attr("checked"));
- });
- $("#Button9").click(function () {
- //多个值,直接.val()默认第一个的值
- $("#divProgram :checkbox:checked").each(function () {
- alert($(this).val());
- });
- });
- $("#Button10").click(function () {
- $("#divProgram :checkbox:checked").each(function () {
- alert($(this).next().text());
- });
- });
- $("#Button11").click(function () {
- //属性过滤器,必须是dom已经有了这个属性,比如虽然select有文本值,可以通过text()方法获取到值,但是不可以通过attr("text")获取,因为没有这个属性
- $("#divProgram :checkbox[value='2']").attr("checked","checked");
- });
- $("#Button12").click(function () {
- $("#divProgram :checkbox").each(function () {
- if ($(this).next().text() == "PHP") {
- $(this).attr("checked", "checked");
- }
- });
- });
- });
- </script>
Jquery操作select,radio,checkBox,获取选中项的值和文本,根据值和文本设置选中项
最新推荐文章于 2024-06-28 10:47:15 发布