通过选择器使用 jquery 获取 html 表单值
get html form value using jquery through selectors
<form id="Task1_1_1">
Keywords: <input type="text" name="twitKey" value="iphone"><br><br>
Coordinates: <input type="text" name="twitLoc" value=""><br> <br>
Scope: <input type="text" name="scope" value="10" > km<br> <br>
<button id="sendButton1_1_1">Search</button>
</form>
这是表格。我可以通过下面的代码获取表单的值。
$('#sendButton1_1_1').on('click', function(e) {
console.log($('#Task1_1_1').val());
});
这是 js 控制台的输出
twitKey=iphone&twitLoc=&scope=10
我的问题是如何使用选择器获取twitKey 的值?
还是我必须自己解析?
我已经知道我通过 id 获得价值
Get the value from HTML form using jquery
我想知道是否可以从值名称中获取值,例如
name="twitKey"
试试 -
console.log($('form#Task1_1_1 input[name=twitKey]').val());
给它加一个class/id -
<input type="text" name="twitKey" id="twitKey" value="iphone" >
和 JS - console.log($('#twitKey').val());
这是一个片段,其中包含多种获取 twitKey 值的方法:
$('input[name="twitKey"]').val(); //iphone
$('input:first').val(); //iphone
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="Task1_1_1">
Keywords: <input type="text" name="twitKey" value="iphone"><br><br>
Coordinates: <input type="text" name="twitLoc" value=""><br> <br>
Scope: <input type="text" name="scope" value="10" > km<br> <br>
<button id="sendButton1_1_1">Search</button>
</form>
<form id="Task1_1_1">
Keywords: <input type="text" name="twitKey" value="iphone"><br><br>
Coordinates: <input type="text" name="twitLoc" value=""><br> <br>
Scope: <input type="text" name="scope" value="10" > km<br> <br>
<button id="sendButton1_1_1">Search</button>
</form>
这是表格。我可以通过下面的代码获取表单的值。
$('#sendButton1_1_1').on('click', function(e) {
console.log($('#Task1_1_1').val());
});
这是 js 控制台的输出
twitKey=iphone&twitLoc=&scope=10
我的问题是如何使用选择器获取twitKey 的值? 还是我必须自己解析? 我已经知道我通过 id 获得价值 Get the value from HTML form using jquery
我想知道是否可以从值名称中获取值,例如
name="twitKey"
试试 -
console.log($('form#Task1_1_1 input[name=twitKey]').val());
给它加一个class/id -
<input type="text" name="twitKey" id="twitKey" value="iphone" >
和 JS - console.log($('#twitKey').val());
这是一个片段,其中包含多种获取 twitKey 值的方法:
$('input[name="twitKey"]').val(); //iphone
$('input:first').val(); //iphone
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="Task1_1_1">
Keywords: <input type="text" name="twitKey" value="iphone"><br><br>
Coordinates: <input type="text" name="twitLoc" value=""><br> <br>
Scope: <input type="text" name="scope" value="10" > km<br> <br>
<button id="sendButton1_1_1">Search</button>
</form>