如何用jquery获取input输入框中的值

如题所述

<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<script src="https://cdn.bootcss.com/jquery/1.8.0/jquery.js "></script>
</head>
<body>
<input id="myText" name="myName" type="text"/>
<button onclick="getVal()">获取值</button>
<script type="text/javascript">
function getVal(){
    var myVal = $("#myText").val();//jq通过id获取input值
    alert('通过id获取值为:'+myVal);
     
    var myVal2 = $("input[name='myName']").val(); //jq通过name获取input值
    alert('通过name获取值为:'+myVal2);
}
 
</script>
</body>
</html>

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答