gpt4 book ai didi

javascript - 如何在javascript中使用id获取文本框的值

转载 作者:行者123 更新时间:2023-11-30 06:53:43 24 4
gpt4 key购买 nike

我想根据id和name获取输入框的值。

因为我的 id 有逗号,所以不接受。当我删除逗号和其中一个 id 时,它显示完美。

但是,我只会将 ID 设为“Text,Demo_1”。我如何根据这个 id 获取值??

这是代码

HTML

<div id="Text,Demo_1" class="span12">
<label>Notes or Concerns</label>
<div class="control-group">
<input type="text" value="hi" name="view1" class="recommend">
<input type="text" value="hi2" name="view1" class="recommend">
<input type="text" value="hi3" name="view1" class="recommend">
</div>
</div>

Js部分

$(function() {
var values = $('#Text,Demo_1 input[name="view1"]').map(function() {
return this.value;
}).get();
alert(values);
});

最佳答案

按名称获取值:

$("[name='view1']").val()

通过 ID 获取值:

$("#[textbox_ID]").val()

获取数组中的所有文本值

var inputTypes = [];

$('.control-group input[name="view1"]').each(function(){
inputTypes.push($(this).val());
});

关于javascript - 如何在javascript中使用id获取文本框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21749856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com