js怎么获取表格中的值

JavaScript09

js怎么获取表格中的值,第1张

1、简单的,表格中的一行添加id属性

1

var item=document.getElementById("id")

2、在行本身绑定方法将自身作为参数传递

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<html>

<head>

<script type = 'text/javascript'>

var curRow //全局行号

var curRowId//选中行的记录信息的ID

var curColor

function selectRow(tr){//tr行本身

curRow = tr

curRowId = tr.id

alert(tr.cells[0].innerText)

}

</script>

</head>

<body onload = "javascript:selectRow(1)">

<table border = "1 solid">

<tr onclick = "selectRow(this)">

<td>001</td>

<td>google</td>

</tr>

<tr onclick = "selectRow(this)

下拉控件:<select id="selectId"></select>

如果你要获取的是动态生成所有的值,用如下方法:

var obj = document.getElementById('selectId')

var options = obj.options

for(var i=0,len=options.lengthi<leni++){

    var opt = options[i]

    alert(opt.value + '----' + opt.text)

}

如果你只要获取当前选择的值:

document.getElementById("selectId").value

对于单选下拉列表,value属性可以直接获取其选中项的value值。

下面实例演示——单击按钮获取下拉列表选中项的value值:

1、HTML结构

<select id="test">

<option value="1">option-1</option>

<option value="2">option-2</option>

<option value="3">option-3</option>

<option value="4">option-4</option>

</select> <br>

<input type='button' value='获取选中项' onClick="fun()"/>

2、javascript代码

function fun(){

obj = document.getElementById("test")

alert(obj.value)

}

3、效果演示