Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery:如何测试select是否已更改或表单是否已提交?_Jquery - Fatal编程技术网

jQuery:如何测试select是否已更改或表单是否已提交?

jQuery:如何测试select是否已更改或表单是否已提交?,jquery,Jquery,如何做到这一点?因为我不想为:$('select').change(function(){})或$('form').submit(function(){}) 编辑:我的目标 我有一个允许用户输入类别代码(4个数字)的第一个表单。当它们开始在字段中写入时,AJAX请求使用与用户编写的代码相对应的类别名称完成选择。在此之后,如果他们有一个正确的类别(在数据库中找到),并且他们按键盘上的enter键,或者如果他们在select中选择了一个类别,我想用与choosen类别相关的数据库的一些值创建一个表单

如何做到这一点?因为我不想为:
$('select').change(function(){})
$('form').submit(function(){})

编辑:我的目标

我有一个允许用户输入类别代码(4个数字)的第一个表单。当它们开始在字段中写入时,AJAX请求使用与用户编写的代码相对应的类别名称完成选择。在此之后,如果他们有一个正确的类别(在数据库中找到),并且他们按键盘上的enter键,或者如果他们在select中选择了一个类别,我想用与choosen类别相关的数据库的一些值创建一个表单(通过另一个AJAX请求获取)。但我不知道如何测试他们是否在按下Enter键(submit按钮隐藏)时提交表单,或者他们是否在select中选择了一个类别

HTML代码

<div class="page-formulaire">
    <form method="post" action="#" id="formulaire-uf">
        <div id="recherche-uf">
            <label>Chose your category </label><br>
            <input type="text" id="uf" maxlength="4" /><br><br>
            <span id="compteur"></span>
            <select name="uf" id="uf-resultat">
                <option selected="selected" id="option-vide"></option>
            </select>

            <div id="uf-erreur"></div>
        </div>
    </form>
    <form method="post" action="#" id="formulaire-maj">

        <div id="formulaire">

            <fieldset>
                <legend>Morning: </legend>
                <label>Man : </label><input type="text" name="hommes-matin" id="hommes-matin" class="input"/><br>
                <label>Women : </label><input type="text" name="femmes-matin" id="femmes-matin" class="input"/><br>
                <label>Twice : </label><input type="text" name="indiff-matin" id="indiff-matin"/><br>
            </fieldset><br>

            <input type="submit" value="Submit" class="btn-bleu">
            <button type="button" id="annuler" onclick="window.location='formulaire.php'">Cancel</button>

        </div>
    </form>

选择您的类别


早晨: 男:
女性:
两次:

取消
给定一个简单的
选择

<select>
    <option value="1">Hello</option>
        <option value=2>Hello2</option>
</select>
.on()
方法中的第一个参数是字符串或普通对象:

一种对象,其中字符串键表示一个或多个空格分隔的事件类型和可选名称空间,值表示要为事件调用的处理程序函数


您只需验证正在侦听的事件是否已注册/适当的事件('select,form')。在('change submit',handler)
?如果两者都在执行相同的代码,为什么不将公共代码放入函数中,让
change
submit
执行该函数。@A.Wolff如果我想测试你的代码,我就这样做?:$('select,form').on('change submit',handler){console.log(“test ok”);}好的,我照你说的做了,我在函数中添加了这一行:$('page formulaire').css('height','1300px');但是现在,如果我只是点击selecte来更改值,而没有选择一个选项,它会显示新的CSW您正在尝试做什么,以及您不希望它做什么?我在你最初的问题中没有看到这一点。好的,我有第一个表单,允许用户输入一个类别的代码,AJAX请求将在select中返回找到的类别名称。我想做第二个表单,当他在select上选择他的类别时,或者如果他在输入完整数字后按键盘的enter键,则使用类别中的值。我认为你应该在StafkOverflow上发布另一个问题,并尝试非常清楚你有什么代码以及你想做什么。这是一个不同的问题。听起来您想要/拥有一个用于
Enter
keypress的事件处理程序?如果我的答案不能回答你的第一个问题,请告诉我,我会删除它。
$('select, form').on('change submit', function(){
   alert("hi"); 
});