Skip to content

Clicking button[type=button] shouldn't submit form when clicked #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ steal('./synthetic.js', function (Syn) {
}

//submit a form
if (!(Syn.support.clickSubmits) && (nodeName === "input" &&
type === "submit") ||
nodeName === 'button') {
if (!(Syn.support.clickSubmits) && ((nodeName == "input" ||
nodeName == 'button') && type == "submit")) {

var form = Syn.closest(element, "form");
if (form) {
Expand Down
26 changes: 24 additions & 2 deletions test/qunit/mouse_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ steal("src/synthetic.js", function (Syn) {
"<input type='radio' name='radio' value='radio2' id='radio2'/>" +
"<a href='javascript:doSomething()' id='jsHref'>click me</a>" +
"<a href='#aHash' id='jsHrefHash'>click me</a>" +
"<button type='button' id='button'></button>" +
"<button type='submit' id='submitButton'></button>" +
"<input type='submit' id='submit'/></div></form>";
},

Expand Down Expand Up @@ -57,10 +59,11 @@ steal("src/synthetic.js", function (Syn) {
return false;
};
st.bind(st.g("outer"), "submit", submitf);
Syn.trigger("click", {}, st.g("submitButton"));
Syn.trigger("click", {}, st.g("submit"));
Syn("submit", {}, "outer");

equal(submit, 2, "Click on submit");
equal(submit, 3, "Click on submit");

//make sure clicking the div does not submit the form
var click = 0,
Expand All @@ -75,12 +78,31 @@ steal("src/synthetic.js", function (Syn) {

Syn.trigger("click", {}, st.g("submit"));

equal(submit, 2, "Submit prevented");
equal(submit, 3, "Submit prevented");
equal(click, 1, "Clicked");

st.unbinder("outer", "submit", submitf);
st.unbinder("inner", "click", clickf);
});

test("Click button[type=button] doesn't submit form", function() {
var submit = 0,
submitf = function (ev) {
submit++;
if (ev.preventDefault) {
ev.preventDefault();
}
ev.returnValue = false;
return false;
};
st.bind(st.g("outer"), "submit", submitf);
Syn.trigger("click", {}, st.g("button"));

equal(submit, 0, "No submit called");

st.unbinder("outer", "submit", submitf);
});

test("Click Checkboxes", function () {
var checkbox = 0;

Expand Down