【问题标题】:get html form value using jquery through selectors [duplicate]通过选择器使用jquery获取html表单值[重复]
【发布时间】:2015-05-13 12:17:48
【问题描述】:
<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"

【问题讨论】:

  • 输入[name='twitkey']

标签: javascript jquery html forms selector


【解决方案1】:

试试 -

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] 来自 id 为“Task1_1_1”的表单?
【解决方案2】:

这里是一个sn-p,有多种方法来获取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>

【讨论】:

    猜你喜欢
    • 2021-05-02
    • 2013-02-20
    • 1970-01-01
    • 2011-11-22
    • 2013-07-04
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多