最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Onclick event on input type image submits form - Stack Overflow

programmeradmin4浏览0评论

I got an input type image with a javascript onclick event attached to it. Everytime I click the image it submits the form. I've already added the "return false", buts that that doesn't solve the problem. Here is my code:

<input type="image" onclick="incrementValue(@i)" value="+" />

Javascript

function incrementValue(n) {

    var id = 'txtAantal' + n;
    var value = parseInt(document.getElementById(id).value, 10);
    value = isNaN(value) ? 0 : value;
    value++;
    document.getElementById(id).value = value;

    var totaal = parseFloat(document.getElementById('txtTotaal').value, 10);
    var prijs = parseFloat(document.getElementById('txtPrice' + n).value, 10);

    document.getElementById('txtTotaal').value = totaal + prijs;

    return false;
}

I got an input type image with a javascript onclick event attached to it. Everytime I click the image it submits the form. I've already added the "return false", buts that that doesn't solve the problem. Here is my code:

<input type="image" onclick="incrementValue(@i)" value="+" />

Javascript

function incrementValue(n) {

    var id = 'txtAantal' + n;
    var value = parseInt(document.getElementById(id).value, 10);
    value = isNaN(value) ? 0 : value;
    value++;
    document.getElementById(id).value = value;

    var totaal = parseFloat(document.getElementById('txtTotaal').value, 10);
    var prijs = parseFloat(document.getElementById('txtPrice' + n).value, 10);

    document.getElementById('txtTotaal').value = totaal + prijs;

    return false;
}
Share Improve this question asked Aug 29, 2014 at 23:45 BoussBouss 2052 gold badges9 silver badges22 bronze badges 1
  • You need to “pass on” the return value: onclick="return incrementValue(@i)" – C3roe Commented Aug 29, 2014 at 23:51
Add a ment  | 

3 Answers 3

Reset to default 4

Try this:

<input type="image" onclick="return incrementValue(@i)" value="+" />

Try returning false after running the function:

<input type="image" onclick="incrementValue(@i); return false;" value="+" />
<button type="button" onclick="incrementValue(@i)" value="+" >                 
<img border="0" src="print.png" width="32" heigth="32" title="Click Me"/>
</button>

You should use a button and use type attribute like this

发布评论

评论列表(0)

  1. 暂无评论