Jquery 选择选项并更改类别ul li

Jquery 选择选项并更改类别ul li,jquery,Jquery,我的代码 <select id="jform_parent_id" name="jform[parent_id]"> <option value="1">Public</option> <option value="8">- Admingruppe</option> <option value="9">- Guest</option> <op

我的代码

 <select id="jform_parent_id" name="jform[parent_id]">
        <option value="1">Public</option>
        <option value="8">- Admingruppe</option>
        <option value="9">- Guest</option>
        <option value="2">- Projektgruppe</option>
    </select>
    <ul class="chzn-results">
        <li class="active-result result-selected" data-option-array-index="0">Public</li>
        <li class="active-result" data-option-array-index="1">- Admingruppe</li>
        <li class="active-result" data-option-array-index="2">- Guest</li>
        <li class="active-result" data-option-array-index="3">- Projektgruppe</li>
    </ul>

公开的
-行政人员
-客人
-项目组
  • 公共
  • -Admingruppe
  • -来宾
  • -Projektgruppe
我想: 选择选项值=2 将选定的类结果添加到ul li数据选项数组index=“3” 删除在其他地方选择的类结果


谢谢你的帮助,这比你想象的要容易。您需要做的只是:

$(function () {
  // On change of the select...
  $("#jform_parent_id").change(function () {
    // Get the current selected index.
    var sIndex = this.selectedIndex;
    // Now find the <li> with correct data-option-array-index and add the class.
    $(".result-selected").removeClass("result-selected");
    $('li[data-option-array-index="' + (sIndex) + '"]').addClass("result-selected");
  });
});

公开的
-行政人员
-客人
-项目组
  • 公共
  • -Admingruppe
  • -来宾
  • -Projektgruppe

这比你想象的要容易。您需要做的只是:

$(function () {
  // On change of the select...
  $("#jform_parent_id").change(function () {
    // Get the current selected index.
    var sIndex = this.selectedIndex;
    // Now find the <li> with correct data-option-array-index and add the class.
    $(".result-selected").removeClass("result-selected");
    $('li[data-option-array-index="' + (sIndex) + '"]').addClass("result-selected");
  });
});

公开的
-行政人员
-客人
-项目组
  • 公共
  • -Admingruppe
  • -来宾
  • -Projektgruppe

。让我们看看你到目前为止都做了些什么。我们很乐意为您提供帮助。请花些时间阅读帮助页面,特别是命名的部分,更重要的是,请阅读。您可能还想了解。。让我们看看你到目前为止都做了些什么。我们很乐意为您提供帮助。请花些时间阅读帮助页面,特别是命名的部分,更重要的是,请阅读。你可能还想了解。非常感谢Praveen Kumar!但我需要在页面加载时预先选择值2,我希望它是我的默认选择value@ErickBoileau使用这种方式
-Projektgruppe
。我现在将更新我的答案。@ErickBoileau检查我的答案。谢谢,但这是我的问题,如何将选定项添加到值2?我不知道为什么它在这里工作,也不在我的网站上。我无法在您的代码中找到您如何选择选项值=2。无论如何,谢谢!非常感谢Praveen Kumar!但我需要在页面加载时预先选择值2,我希望它是我的默认选择value@ErickBoileau使用这种方式
-Projektgruppe
。我现在将更新我的答案。@ErickBoileau检查我的答案。谢谢,但这是我的问题,如何将选定项添加到值2?我不知道为什么它在这里工作,也不在我的网站上。我无法在您的代码中找到您如何选择选项值=2。无论如何,谢谢!