简体   繁体   English

jQuery如何确保用户已为表单上的所有选择菜单选择一个值

[英]jquery how to make sure that the user has selected a value for all select menus on a form

i have a form with multiple select menus i want to make sure on submit that a user selected a value for each select menu how can i do that with jquery ? 我有一个带有多个选择菜单的表单,我想确保在提交时用户为每个选择菜单选择了一个值,该如何使用jquery做到这一点? i tried something like 我尝试过类似的东西

var form = $('myform');
if($(form ).find('select').length != $(form).find('select:option[selected="selected"]').length ) { 
    alert('wrong please make sure to select all select menu');
}

but no luck 但没有运气

please help 请帮忙

Thank you 谢谢

The form object has already been created.I removed the $() function from the form variables in the if statement. 表单对象已经创建。我从if语句的表单变量中删除了$()函数。 You should also be using an identifier for myform . 您还应该为myform使用标识符。

var form = $('form#myform');
if(form.find('select').length != form.find('select:option[selected="selected"]').length ) { 
    alert('wrong please make sure to select all select menu');
}

I recommend you prefix your form var with a $ to note that you are already using a jquery object. 我建议你的前缀form变种与$要注意,你已经在使用jQuery对象。

Example: 例:

var $form = $('form#myform');
if($form.find('select').length != $form.find('select:option[selected="selected"]').length ) { 
    alert('wrong please make sure to select all select menu');
}

By default select element's first option is selected if none of the options have selected attribute, so your selector will not work which is looking for selected attribute in the option element. 默认情况下,如果所有选项都没有selected属性,则选择元素的第一个选项处于selected ,因此您的选择器将不起作用,该选项在选项元素中查找selected属性。 Also if the id of the form is myform then you should use #myform to find it. 另外,如果表单的idmyform ,则应使用#myform进行查找。 Try this. 尝试这个。

var $form = $('#myform');
if($form.find('select').length != $form.find('select:option:selected').length ) { 
    alert('wrong please make sure to select all select menu');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery确保填写所有表单字段 - jQuery make sure all form fields are filled 如何验证表单字段集是否已选中所有(如果有的话)选择框? - How to validate that a form fieldset has all, if any, select boxes selected? 如何使用jquery检查所有选择框是否都选择了选项? - How to check if all select boxes has selected option using jquery? 如何确保在表单内选择了所有文本字段和单选按钮? - How do i make sure ALL textfields & Radio Buttons are selected inside a form? 确保选择的值显示在第一个选择输入字段中 - Make sure selected value is shown in first select input field MVC:如何确保我的字段在编辑表单的视图内选择了值? - MVC: How can I make sure that my field have the value selected inside my view on the edit form? 如何在表单中的多个选择类型下拉菜单中使用jQuery脚本? - How to use jQuery script for multiple select type dropdown menus in a form? 如何确保已选择单选按钮 - How to make sure a radio button has been selected 在Vue.js中,如何确保select元素连续选择了第一个选项,因为有条件地显示和隐藏了选项? - In Vue.js, how can I make sure a select element continuously has the first option selected as options are conditionally shown and hidden? 如何确保在提交多页表单之前至少选中了一个复选框? - How to make sure at least 1 checkbox is selected before submitting multipage form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM