选择占位符选项时,选择Jquery切换类

时间:2018-03-18 15:10:25

标签: javascript jquery html html-select

我是JQuery的新手,尽管我的研究和解决方法,我还没有找到正确的方法来编写我想在这里实现的目标。

所以,情况就是这样。我有一个这样的下拉列表:

<select name="your-country" class="wpcf7-form-control wpcf7-select wpcf7-validates-as-required" aria-required="true" aria-invalid="false">
    <option value="">Your Country*</option>
    <option value="France">France</option>
    <option value="Belgium">Belgium</option>
    <option value="Switzerland">Switzerland</option>
</select>

每当第一个占位符“Your Country *”选项被选中时,我希望我的下拉列表为灰色,当选择另一个选项时,我希望黑色。

select{
    color: #000;
}
select.my-placeholder{
    color: #666 !important;
}

我想创建一个简单的jquery代码块,只要选择了第一个选项(没有值),就会添加类.my-placeholder,并在选择其他选项时丢弃它。 (我想在HTML代码中无变化

我怎样才能实现这一目标? 谢谢

1 个答案:

答案 0 :(得分:0)

您可以创建一个响应HTTP Error 502.5 - Process Failure Common causes of this issue: The application process failed to start The application process started but then stopped The application process started but failed to listen on the configured port Troubleshooting steps: Check the system event log for error messages Enable logging the application process' stdout messages Attach a debugger to the application process and inspect For more information visit: https://go.microsoft.com/fwlink/?LinkID=808681 事件的函数,并选择检查值并添加相应的类

&#13;
&#13;
change
&#13;
function updateColor() {
  let getSelectedValue = $('select[name="your-country"]').val();
  if (getSelectedValue === "") {

    $('select[name="your-country"]').find('option').addClass('my-placeholder');
  } else {

    $('select[name="your-country"]').find('option').removeClass('my-placeholder').addClass('select');

  }
}
&#13;
.select {
  color: red;
}

.my-placeholder {
  color: #666;
}
&#13;
&#13;
&#13;