<!DOCTYPE html>
<html>
<script language="javascript">
function getValue(){
var list = document.getElementsByTagName("input");
var inputList = document.getElementsByName("lastname");
for(i=0;i<inputList.length;i++){
alert(inputList[i].value);
}
}
</script>
<body>
First name:<br>
<input type="text" name="firstname" value ="maggie">
<br>
Last name:<br>
<input type="text" name="lastname" value= "hello" οnclick=getValue()>
<br><br>
</form>
<p>如果您点击提交,表单数据会被发送到名为 demo_form.asp 的页面。</p>
</body>
</html>
运行结果:
hello
可以用
document.getElementsByName("lastname") 拿到一系列的lastname的value
还有一种办法就是
$("input[name='userName']").
<input type="text" name="userName" /> <script type="text/javascript"> $(function(){ var userName = $("input[name='userName']").val(); }); </script>