通过选择器使用jQuery获取HTML表格值


<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中获得了价值使用jQuery

从HTML表单中获取值

我想知道我是否可以从

之类的价值名称中获得值
name="twitKey"

尝试 -

console.log($('form#Task1_1_1 input[name=twitKey]').val());

向其添加类/ID-

<input type="text" name="twitKey" id="twitKey" value="iphone" >

和JS -console.log($('#twitKey').val());

这是一个摘要

$('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>

最新更新