JQ绑定select标签的onchange事件,跳转、传参、添加、删除

JQ绑定select标签的onchange事件,跳转、传参、添加、删除 

  • <select id="mySelect">  
      <option value="1">one</option>  
      <option value="2" selected="selected">two</option>  
      <option value="3">three</option>  
    </select>
    <input type="text" value="ooo" name="param2" id="param2"/> 
    
    <script src="jquery.min.js"></script>  
    <script>  
        $('#mySelect').change(function(){  
          alert($(this).children('option:selected').val());  
          var p1=$(this).children('option:selected').val();//这就是selected的值  
          var p2=$('#param2').val();//获取本页面其他标签的值  
          window.location.href="xx.php?param1="+p1+"m2="+p2+"";//页面跳转并传参  
        })  
    </script>

jquery获取select选择的文本与值

获取select 选中的 text :
$("#ddlregtype").find("option:selected").text();
获取select选中的 value:
$("#ddlregtype ").val();
获取select选中的索引:
$("#ddlregtype ").get(0).selectedindex;
运用new Option("文本","值")方法添加选项option :
var obj = document.getElementById("mySelect");  
obj.add(new Option("4","4")); 
删除所有选项option:

var obj = document.getElementById("mySelect");  
obj.options.length = 0;
删除选中选项option:
var obj = document.getElementById("mySelect");  
var index = obj.selectedIndex;  
obj.options.remove(index); 
修改选中选项option
var obj = document.getElementById("mySelect");  
var index = obj.selectedIndex;  
obj.options[index] = new Option("three",3); //更改对应的值  
obj.options[index].selected = true; //保持选中状态 
删除select:
var obj = document.getElementById("mySelect");  
obj.parentNode.removeChild(obj); //移除当前对象 
select选择的响应事件:
$("#mySelect").change(function(){  
//添加所需要执行的操作代码  
}) 

猜你喜欢

转载自blog.csdn.net/qq_32444825/article/details/81157085
今日推荐