【问题标题】:Javascript onclick image display alt-tag in inputboxJavascript onclick图像在输入框中显示alt-tag
【发布时间】:2013-11-24 13:41:20
【问题描述】:

我收到了一个关于带有图像的 onclick 事件的基本问题。 我有四个图像是有价格的产品。价格在 Alt 标记中(为简单起见,现在全部 100)。请参阅下面的 HTML:

<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-tag。输入“total”应该能够显示被点击的所有图片(产品)的总价。

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

问候

【问题讨论】:

    标签: javascript image input onclick alt


    【解决方案1】:

    使用jQuery,编辑你的html成为(添加一个'id'参数):

    <img src="product1.png" alt="100" onclick="pricecheck(1);" id="product1" />
    <img src="product2.png" alt="100" onclick="pricecheck(2);" id="product2" />
    

    你的 jQuery 函数:

    function pricecheck(id){
       $('input[name="price"]').val($('img#product' + id).attr('alt'));
    }
    

    使用纯 JavaScript。

    首先编辑您的输入标签:&lt;input type="text" name="price" id="price" /&gt;

    function pricecheck(id){
       var img = document.getElementById('product' + id);
       document.getElementById('price').value = img.getAttribute("alt");
    }
    

    【讨论】:

    • 感谢您的回答,但问题是我不能为此使用 jQuery,我必须使用 Javascript。
    猜你喜欢
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 2018-01-13
    相关资源
    最近更新 更多