简体   繁体   English

如何在不提交整个表单的情况下单击输入类型“提交”按钮

[英]How to click on an input type“submit” button without submiting the whole form

I am trying to click the following line of code: 我试图单击以下代码行:

<input title="Add To Cart" name="pdp_addtocart" value="Add To Cart" type="submit" class="active_step">

I have tried the .click() form and it doesnt work. 我尝试了.click()表单,但它不起作用。 I need the code in javascript. 我需要javascript中的代码。

Try adding the onclick event to the button: 尝试将onclick事件添加到按钮:

<input onclick="return false;".... />

return false in the event will halt the form submission. 如果返回false,则将停止表单提交。

or using pure javascript: 或使用纯JavaScript:

theButton.onclick = function() {
    return false;
};

Edit: 编辑:

jQuery version: jQuery版本:

$theBtn.click(function() {
    return false;
});

With Jquery : 使用Jquery

var my_btn = $('input[name="pdp_addtocart"]');

my_btn.click(function(event){
 event.preventDefault(); //This avoid  the default behavior of the button
  //Your Stuff
});

In case your form still sending add this line: 如果您的表单仍在发送,请添加以下行:

   $('form').submit(function(event){
     event.preventDefault(); //This avoid the behavior of sending the form
    });

Does it have to be a <input type="submit" /> ? 它必须是<input type="submit" />吗?

You could use a <input type="button" /> instead. 您可以改用<input type="button" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM