首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript图像显示inputbox中的alt-tag

问Javascript图像显示inputbox中的alt-tag
EN

Stack Overflow用户
提问于 2013-11-24 13:41:20
回答 1查看 1.5K关注 0票数 0

我有一个关于带有图片的onclick事件的基本问题。我有四张图片,它们都是有价格的产品。价格是在Alt-标签(为了简单,现在全部100 )。请参见下面的HTML:

代码语言:javascript
复制
<img src="product1.png" alt="100" onclick="pricecheck();" id="product1" />
<img src="product2.png" alt="100" onclick="pricecheck();" id="product2" />
<img src="product3.png" alt="100" onclick="pricecheck();" id="product3" />
<img src="product4.png" alt="100" onclick="pricecheck();" id="product4" /> <br />

<label for="Price" >Price:</label><input type="text" name="price"/><br />            
<label for="totaal">Total:</label><input type="text" name="total"/>

我只是不知道如何做一个函数,当你点击图片时,它会显示输入“价格”中的alt标签。输入的“总计”应该能够显示所有点击的图像(产品)的总价格。

希望你们能帮我解决这个问题。

问候

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-24 14:00:05

使用jQuery,编辑html变成(添加一个'id‘参数):

代码语言:javascript
复制
<img src="product1.png" alt="100" onclick="pricecheck(1);" id="product1" />
<img src="product2.png" alt="100" onclick="pricecheck(2);" id="product2" />

jQuery函数:

代码语言:javascript
复制
function pricecheck(id){
   $('input[name="price"]').val($('img#product' + id).attr('alt'));
}

使用纯javascript。

首先编辑输入标签:<input type="text" name="price" id="price" />

代码语言:javascript
复制
function pricecheck(id){
   var img = document.getElementById('product' + id);
   document.getElementById('price').value = img.getAttribute("alt");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20175173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档