我有一个HTML表单,我使用几个按钮.问题是无论我点击哪个按钮,即使按钮不是"提交"类型,表单也会被提交.例如:按钮<button>Click to do something</button>,导致表单提交.
e.preventDefault()为这些按钮中的每一个做一个是非常痛苦的.
我使用jQuery和jQuery UI,网站是HTML5.
有没有办法禁用这种自动行为?
哲学问题:
假设我有一个需要 javascript和现代浏览器的网络应用程序,因此渐进式增强不是问题.如果我的表单是通过javascript构建的,并且我的数据更新都是通过ajax POST和PUT完成的,那么我真的有理由将我的控件包装在表单标签中吗?如果我仍然要使用标签,比如说出于语义或结构原因,是否有任何理由要采取我将忽略的动作和方法参数?从我早期的时代开始,这种感觉就像是一种拖延.
如何保留默认的HTML表单验证并禁止表单提交?我试过用event.preventDefault()和return false.两者都成功地阻止了表单的提交,但都失败了表单验证.我也试过这个解决方案,但即使这样也行不通.
有人能帮帮我吗?
我是ReactJS的新手,我制作了一个应用程序,您可以在其中提交名称和电子邮件。名称和邮件应显示在页面底部的列表中。它显示一小段时间,但是随后构造函数被调用并清除状态和列表。
为什么在状态更改后调用构造函数?我以为构造函数只运行一次,然后render-method在setState()更改状态后运行。
class App extends React.Component {
constructor(props) {
super(props);
console.log("App constructor");
this.state = {
signedUpPeople: []
};
this.signUp = this.signUp.bind(this);
}
signUp(person) {
this.setState({
signedUpPeople: this.state.signedUpPeople.concat(person)
});
}
render() {
return (
<div>
<SignUpForm signUp={this.signUp} />
<SignedUpList list={this.state.signedUpPeople} />
</div>
);
}
}
class SignUpForm extends React.Component {
constructor(props) {
super(props);
console.log("SignUpForm constructor");
this.state = {
name: "",
email: ""
};
this.changeValue = this.changeValue.bind(this);
this.onSubmitForm = this.onSubmitForm.bind(this);
}
changeValue(event) {
const value …Run Code Online (Sandbox Code Playgroud) 我的 React 项目中有一个表单。confirm()如果用户单击提交按钮,我想打电话。我尝试过这样的:
<form onSubmit="return confirm('Are you sure?')">...</form>
Run Code Online (Sandbox Code Playgroud)
<button type="submit" onClick="return confirm('Are you sure?')">Submit</button>
Run Code Online (Sandbox Code Playgroud)
<form onSubmit={()=>window.confirm('Are you sure?')}>...</form>
Run Code Online (Sandbox Code Playgroud)
<button onClick={() => window.confirm("Are you sure")}>Submit</button>
Run Code Online (Sandbox Code Playgroud)
不幸的是,没有任何反应,表单在没有警报的情况下提交。React 以某种方式删除了onClick和onSubmit。如何才能避免这种情况呢?
javascript ×5
forms ×3
html ×2
reactjs ×2
ajax ×1
button ×1
form-submit ×1
jquery ×1
validation ×1