我有几个链接的下拉列表,如果链接的下拉列表为空,我想阻止表单提交更改事件。
<form id="foo">
<select name="first" on="change:AMP.setState({settings: {first: event.value}}),foo.submit" >
...
</select>
<select name="second" on="change:AMP.setState({settings: {second: event.value}}),foo.submit" >
<option value=0> Please select </option>
...
</select>
</form>
如果值合适,我可以检查状态然后提交表单吗?就像是:
<select name="first" on="change:AMP.setState({settings: {first: event.value}}),settings.second ? foo.submit : nothing to do" >
当我选择新值时,第一个下拉列表可以避免表单提交,因为第二个还没有被选中。
尝试通过验证来实现,但它不适用于下拉菜单,输入工作正常。
<input type="text"
id="show-first-on-submit-name"
name="name"
placeholder="Digit..."
required
on="change:AMP.setState({settings: {testing: event.value}}),requirements-form.submit"
pattern="^([1-9][0-9]*)$">
<span visible-when-invalid="valueMissing"
validation-for="show-first-on-submit-name"></span>
<span visible-when-invalid="patternMismatch"
validation-for="show-first-on-submit-name">
Please enter not '0'
</span>
<select name="purpose"
id="purpose"
pattern="^([1-9][0-9]*)$"
required
on="change:AMP.setState({settings: {purpose: event.value}}),requirements-form.submit">
<option value="0">Please select</option>
<option value="1">One</option>
</select>
<span visible-when-invalid="valueMissing"
validation-for="purpose">missing</span>
<span visible-when-invalid="patternMismatch"
validation-for="purpose">wrong</span>