如何在 jquery 中将输入 id 标记声明为变量?
$('<p><input type="text" id = "count" value="Enter Item Name"/></p>')
$('#counted').text(count);
$('#word').text($('#count').val());
计数为:
Word 是:
我想做的是打印出当前输入标记为 count 的内容。有多个具有名称计数的输入。我不知道我应该如何去做这件事。
$('<p><input type="text" id = "count" value="Enter Item Name"/></p>')
$('#counted').text(count);
$('#word').text($('#count').val());
Count is: <span id = "counted"></span>
Word is: <span id = "word"></span>
what i'm trying to do is to print out what is in the current input labeled count. there are more than one inputs with the name count. i'm not sure how i am supposed to go about doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要添加多个项目,请记住使用类而不是 ID,ID 应始终是唯一的。
如果您想将其推送到数组,您可以使用类似的内容(这是一个使用“更新”链接创建数组的示例):
In
:
In
;
:您将
.count
计数保存在 itemscount 中,mycountarray
将存储这些输入中的值,即您的值 a、b、c、d '将得到一个数组:["a", "b", "c", "d"]
我不确定这正是您正在寻找的内容,但希望它是:)
干杯G。
Remember to use class not ID if you want to add more than one item, ID should always be unique.
If you want to push it to an array you can use something like this (it's an example with 'Update' link to create the array):
In
<head>
:In
<body>
:You're saving the
.count
count in itemscount andmycountarray
will store values from those inputs, i.e. for values a, b, c, d you'll get an array:["a", "b", "c", "d"]
I'm not sure it that's exactly what you're looking but hopefully it is :)
Cheers G.
考虑在您的问题中提供更多信息。
假设
当前输入标记计数
表示当前选择/聚焦的输入,并且您希望在<内显示该输入的值/code>
jQuery:
这将显示跨度中当前聚焦输入的值,如果没有聚焦(选择)输入,则显示“请选择输入”文本,
这是一个示例: http://jsfiddle.net/nev3rm0re/Lju5U/
Consider providing more information in your question.
Assuming that by
current input labeled count
you mean currently selected/focused input and you want to display value of that input inside<span id="word"></span>
jQuery:
This will display value from currently focused input in span, and display "Please select an input" text if no input is focused (selected)
Here's an example: http://jsfiddle.net/nev3rm0re/Lju5U/