js获取select标签选中的值

菜鸟教程 jsp教程

http://www.runoob.com/jsp/jsp-form-processing.html



js获取select标签选中的值


var obj = document.getElementByIdx_x(”testSelect”); //定位id

var index = obj.selectedIndex; // 选中索引

var text = obj.options[index].text; // 选中文本

var value = obj.options[index].value; // 选中值


jQuery中获得选中select值

第一种方式
$('#testSelect option:selected').text();//选中的文本

$('#testSelect option:selected') .val();//选中的值

$("#testSelect ").get(0).selectedIndex;//索引

 

第二种方式
$("#tesetSelect").find("option:selected").text();//选中的文本
…….val();
…….get(0).selectedIndex;



http://www.cnblogs.com/itdream/archive/2012/05/31/2528345.html

现在有一id=test的下拉框,怎么拿到选中的那个值呢?

分别使用javascript原生的方法和jquery方法

code:

一:javascript原生的方法

  1:拿到select对象: var  myselect=document.getElementById("test");

  2:拿到选中项的索引:var index=myselect.selectedIndex ;             // selectedIndex代表的是你所选中项的index

  3:拿到选中项options的value:  myselect.options[index].value;

  4:拿到选中项options的text:  myselect.options[index].text;

二:jquery方法(前提是已经加载了jquery库)

1:var options=$("#test option:selected");  //获取选中的项

2:alert(options.val());   //拿到选中项的值

3:alert(options.text());   //拿到选中项的文本



http://blog.csdn.net/itmyhome1990/article/details/41849175

 

form表单提交的几种方法


  1. <form id="myform" name="myform" method="post" onsubmit="return sumbitTest();"   
  2.       action="RegisterAction.action">  
  3.     <table>  
  4.         <tr>  
  5.             <td>姓名:td>  
  6.             <td> <input type="text" name="name" /> td>  
  7.         tr>  
  8.         <tr>  
  9.             <td>性别:td>  
  10.             <td>  
  11.                 <input type="radio" name="sex" value="1"> 男  
  12.                 <input type="radio" name="sex" value="0"> 女  
  13.             td>  
  14.         tr>  
  15.         <tr>  
  16.             <td>年龄:td>  
  17.             <td>  
  18.                 <select name="age">  
  19.                     <option value="20">20option>  
  20.                     <option value="21">21option>  
  21.                     <option value="22">22option>  
  22.                 select>  
  23.             td>  
  24.         tr>  
  25.         <tr>  
  26.             <td colspan="2">  
  27.                 <input type="submit" value="Submit普通提交">  
  28.                 <input type="button" id="ajaxBtn" value="AJAX提交" />  
  29.                 <input type="button" id="jqueryBtn" value="jQuery提交" />  
  30.                 <input type="button" id="jsBtn" value="JS提交" />  
  31.                 <input type="submit" value="onSubmit提交" />  
  32.             td>  
  33.         tr>  
  34.     table>  
  35. form>  
[javascript]  view plain  copy
 
  1. "text/javascript">  
  2.     $(function() {  
  3.         //ajax提交  
  4.         $("#ajaxBtn").click(function() {  
  5.             var params = $("#myform").serialize();  
  6.             $.ajax( {  
  7.                 type : "POST",  
  8.                 url : "RegisterAction.action",  
  9.                 data : params,  
  10.                 success : function(msg) {  
  11.                     alert("success: " + msg);  
  12.                 }  
  13.             });  
  14.         })  
  15.   
  16.         //jQuery提交  
  17.         $("#jqueryBtn").click(function(){  
  18.             $("#myform").submit();  
  19.         })  
  20.   
  21.         //js提交   
  22.         $("#jsBtn").click(function(){  
  23.             document.myform.action="RegisterAction.action";     
  24.             document.myform.submit();     
  25.         })  
  26.     })  
  27.     function sumbitTest(){  
  28.         return true//return false则不会提交   
  29.     }  
  30.   
以上是比较常用的几种表单提交方式,但绝不局限于这些


Jquery ajax提交表单几种方法详解

[导读] 在jquery中ajax提交表单有post与get方式,在使用get方式时我们可以直接使用ajax 序列化表单$( 表单ID) serialize();就行了,下面我来介绍两个提交表单数据的方法。$get方式提交表单get() 方法通过远程 HTTP

在jquery中ajax提交表单有post与get方式,在使用get方式时我们可以直接使用ajax 序列化表单$('#表单ID').serialize();就行了,下面我来介绍两个提交表单数据的方法。

$get方式提交表单

get() 方法通过远程 HTTP GET 请求载入信息

格式

$(selector).get(url,data,success(response,status,xhr),dataType)

请求 test.php 网页,传送2个参数,忽略返回值:

$.get("test.php", { name: "John", time: "2pm" } );

显示 test.php 返回值(HTML 或 XML,取决于返回值):

 代码如下 复制代码

$.get("test.php", function(data){
  alert("Data Loaded: " + data);
});


ajax 序列化表单

$.Form.serialize( NameValuePair )

虚拟一个表单,并设置表单控件名与值。

参数
NameValuePair

必选项。设置虚拟的表单控件。该参数形式为:{ name1=value, name2=value2, ......}
返回值

虚拟表单序列化后的字符串,其格式如:username=%E5%95%8A%E8%94%A1&password=123456

 代码如下 复制代码

 
 
 
 
 

 
 



 
 
 
 
 
 
 
 
.serialize() 方法可以操作已选取个别表单元素的 jQuery 对象,比如 ,