Selecting an option by its text

后端 未结 2 1639
礼貌的吻别
礼貌的吻别 2021-01-23 11:42

I\'m developing a web page using django and I want to add some javascript to a form using jQuery. Basically I want to select an option in a form depending on an another selectio

相关标签:
2条回答
  • 2021-01-23 12:14

    You can check like this to get the text of the selected <option> (for your first code block):

    $("#id_adeudado :selected").text();
    

    And this to set the selected <option> value by the text:

    $("#id_adeudado option").filter(function() {
      return this.value == '{{ usuario.username }}';
    }).attr('selected', true);
    
    0 讨论(0)
  • 2021-01-23 12:18

    Just found something that works, using contains instead of the text='values' selector

    not working for me

    $("#id_adeudado option[text='{{ usuario.username }}']")
    

    working

    $("#id_adeudado option:contains('{{ usuario.username }}')")
    
    0 讨论(0)
提交回复
热议问题