javascript - 如何判断表单是通过 Select onChange 提交还是通过 Submit 按钮提交

标签 javascript php html forms

我需要帮助确定表单何时通过 Select 的 onChange 事件提交,而不是用户单击“提交”按钮。

让事情变得复杂的是,我需要能够识别 PHP 中的差异。

示例 HTML:

<form name='editdata' method='post' action='/editpage/recordnumber'>
<select name='select1' onchange='this.form.submit()'><options...></select> 
<select name='select2' onchange='this.form.submit()'><options...></select>
<select name='select3' onchange='this.form.submit()'><options...></select> 
<input type='submit' value='Submit' />
</form>

最终,我只需要使用 PHP 来锁定表单,以识别表单是通过 onChange 事件还是通过按“提交”按钮提交的。

到目前为止,我发现我可以将我的提交按钮命名为“提交”。这将设置 $_POST['submit'],从而告诉我用户单击了该按钮。但是,当我将 name 属性添加到提交按钮时,应用于几个 Select 的 onChange 事件 this.form.submit() 停止运行。

如果我可以使用一种或另一种方法将某种查询字符串添加到提交 URL 的末尾,我认为这就能完成工作。

最佳答案

您可以设置一个隐藏字段,该字段由事件更新:

<form name='editdata' method='post' action='/editpage/recordnumber'>
<input type="hidden" name="submitted_on_change" value="0">
<select name='select1' onchange='form_submit()'><options...></select> 
<select name='select2' onchange='form_submit()'><options...></select>
<select name='select3' onchange='form_submit()'><options...></select> 
<input type='submit' value='Submit' />
</form>

<script>
var form_submit = function() {
   // update the hidden field to 1
   // then submit the form
}
</script>

关于javascript - 如何判断表单是通过 Select onChange 提交还是通过 Submit 按钮提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31439847/

相关文章:

javascript - 如何在 cordova 中使用文件系统

php - 我怎样才能在 Doctrine 中将 NULL 设置到表中

php - 第一次连接阻止了对 PHP 的多个请求

php - 检查 db 值是否与当前 Select 选项匹配的函数

jquery - 下拉菜单 jquery 切换项目

javascript - idTabs 样式缺失?

javascript - 在我的 react shouldComponentUpdate 函数中,this.props 和 nextprops 具有所有相同的属性,但它们不相等

javascript - 创建一条垂直线,覆盖所有 html 内容并在特定时间段内在页面上移动(javascript)

javascript - 向上和向下箭头的导航不适用于 Bootstrap Typeahead

jquery - 是否可以在鼠标悬停时在同一个超链接中打开 2 个链接