简体   繁体   English

选择菜单具有值时禁用复选框

[英]Disabling checkbox when a select menu has values

I have a select menu with about four options. 我有一个选择菜单,有四个选项。 The first option of the select menu has NO value. 选择菜单的第一个选项没有值。 The other options have values. 其他选项有价值。 When a user selects an option that has a value, I need the checkbox disabled, otherwise, if they go back to the option with the blank value, the checkbox is re-enabled. 当用户选择具有值的选项时,我需要禁用该复选框,否则,如果他们返回带有空值的选项,则重新启用该复选框。

The code below successfully disables the checkbox, BUT I want the checkbox to be re-enabled when the user clicks on the first blank option of the select menu. 下面的代码成功禁用了复选框,但是当用户点击选择菜单的第一个空白选项时,我希望重新启用该复选框。

$(document).ready(function(){
$('#select-menu').change(function() {
  if ($(this).val() !== ':first-child') { 
        $("#related-checkbox").attr("disabled", true);
  } else {
    $("#related-checkbox").attr("disabled", false);
            }
});
});

The statement says 声明说

if ($(this).val() !== ':first-child') { 

literally checks if selected value string is 'first-child', not whether it is the first child. 字面上检查所选值字符串是否为“first-child”,而不是它是否是第一个孩子。 Just set it to blank 只需将其设置为空白即可

if ($(this).val() !== '') { 

If you want to check whether it is the first option (not whether it is blank) then you can compare the first child's val to the selected element's val 如果你想检查它是否是第一个选项(不是它是否为空),那么你可以将第一个孩子的val与所选元素的val进行比较

if ($(this).val() == $(this).children(':first-child').val())

您的意思是说:“当用户点击选择菜单的第一个空白选项时,我希望重新启用该复选框。”?

$("#related-checkbox").removeAttr("disabled");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM