简体   繁体   English

如何 Select 某个选项字段

[英]How To Select A Certain Option Field

Jquery is not my main language PHP is. Jquery 不是我的主要语言 PHP 是。

Anyway,反正,

What I am trying to do is detect if a field has been selected my code almost works but the issue is that when it selects the certain option I get a alert which is good but then when I select the dropdown again [since its being selected] I get another alert which I don't want.我想要做的是检测是否选择了一个字段,我的代码几乎可以工作,但问题是当它选择某个选项时,我会收到一个很好的警报,但是当我 select 再次下拉[因为它被选中]我收到另一个我不想要的警报。 What I do want is once the user select the dropdown then clicks the option again I want another alert我想要的是一旦用户 select 下拉菜单然后再次单击该选项我想要另一个警报

$('#_privacy_selection').click(function() {

    if($("#_privacy_selection option:selected").text() == 'Certain Users'){
         alert(9);
         return false;
 }

});

Thanks谢谢

Instead of click function use change function on <option> It will be fired only when you change selected option.而不是click function 使用change function on <option>只有当您更改所选选项时才会触发。

$('#_privacy_selection').change(function() {

    if($("#_privacy_selection option:selected").text() == 'Certain Users'){
         alert(9);
         return false;
    }

});

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

相关问题 如何在没有jQuery的情况下检查select字段是否具有某个选项值 - How to check if a select field has a certain option value without jQuery 如何在一定时间后禁用选择选项? - How to disable select option after a certain time? 如何在select中修改选项字段的大小? - How to modify the size of option field in select? 如何使用vue设置在选择字段中选择的选项? - How to set the option selected in a select field with vue? 如何通过选择选项数量字段正确替换数量的输入字段? - How to correctly replace input field for quantity by select option quantity field? 如何在选择选项字段中选择特定的自定义属性? - How select specific custom attribute in an select option field? Select2:如何在 VueJS 的多个 select 中添加选项搜索字段 - Select2: How to add option Search field in Multiple select in VueJS 如何在过滤后的 html 选择字段中选择第一个选项 - How to select the first option in filtered html select field 如何通过选择 select 标签中的某个选项将 URL 获取到页面 - How to a get a URL to a page with a certain option from select tag selected 当我选择另一个选择标记HTML的某个选项时,如何自动更改选择标记选项的值列表<select> - How to automatically change the value list of option of select tag when i select the certain option of another select tag HTML <select>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM