如何使用它的id更改选项Tag的内容?

时间:2017-10-30 11:45:36

标签: javascript jquery html

这应该是一个简单的问题,但我无法解决,所以我需要你的帮助。

我的select结构中有四个选项标记,每个标记都有一个值,id和内容如下: -

<select id='0'>
      ABC
      <option id='123' value='X'>ABC</option>
      <option id='456' value='Y'>DEF</option>
    </select>

我的问题是,如何在以后更改选项Tag的内容?我不能用它的值选择特定的选项标签,因为我只能通过一系列复杂的函数来获取id。

更清楚: - 我有一个选项标签的ID,我需要更改内容,“ABC”,“DEF”,该值用于其他事情,我无法选择选项标签价值。

谢谢大家!

3 个答案:

答案 0 :(得分:0)

它应该完美!

$('select #id').text('ABC');

$('select option#id').text('ABC');

jquery有不同的方法来更改option的内容,仅作为text()说。然后,剩余的所有html代码都可以使用html()来更改内容。   然后,我选择option specific_id $('select option#specific_id') public class TestCompare { public static void main(String[] args) { Person person1 = new Person(45, "Tom"); Person person2 = new Person(12, "Sarah"); Person person3 = new Person(34, "Michael"); Person person4 = new Person(33, "Donald"); Person person5 = new Person(65, "timothy"); List<Person> people = new ArrayList<Person>(); people.add(person1); people.add(person2); people.add(person3); people.add(person4); people.add(person5); CustomComparator comparator=new CustomComparator(); for (Person p : people) { System.out.println(comparator.compare(p, new Person(55, "James"))); } } } class CustomComparator implements Comparator<Person> { @Override public int compare(Person o1, Person o2) { // TODO Auto-generated method stub return o1.getAge().compareTo(o2.getAge()); } } class Person implements Comparable<Person> { public Person(int age, String name) { this.age = age; this.name = name; } private Integer age; private String name; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int compareTo(Person o) { // TODO Auto-generated method stub return this.getAge().compareTo(o.getAge()); } }

答案 1 :(得分:0)

$(function(){
$('select option[id="123"]').text('xyz');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id='0'>
      <option id='123' value='X'>ABC</option>
      <option id='456' value='Y'>DEF</option>
    </select>

就像这样:

答案 2 :(得分:0)

// Imagine you have optionId as variable
var optionId = '456';

// Get related option element
var relatedOption = $('#select option[id=' + optionId + ']');

// Change its value and its text
relatedOption.val(relatedOption.attr('value')).text(relatedOption.attr('value'))