<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());
添加一个类/id -
<input type="text" name="twitKey" id="twitKey" value="iphone" >
和 JS - console.log($('#twitKey').val());
关于javascript - 使用jquery通过选择器获取html表单值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30205433/