jQuery's val() method change doesn't seem to change the DOM

后端 未结 4 1126
青春惊慌失措
青春惊慌失措 2021-01-18 04:37

Doing $(\"#someId\").val(\"newValue\") doesn\'t change the DOM -- I can retrieve this value with $(\"#someId\").val(), but the element in the DOM s

4条回答
  •  春和景丽
    2021-01-18 04:59

    Reading from "rsp" answer and discussion:

    you want to change the HTML source, not the DOM.

    Therefore use:

    $('#YOUR_ID option[value="YOUR_VALUE"]').attr('selected', 'selected');
    

    The method .val() does not change the HTML source.

    Cheers. Stefano

提交回复
热议问题