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

后端 未结 4 1130
青春惊慌失措
青春惊慌失措 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条回答
  •  萌比男神i
    2021-01-18 04:55

    .val() does change the DOM. For example this:

    $("#someId").val("newValue");
    
    alert(document.getElementById('someId').value);
    

    alerts 'newValue'.

    See DEMO.

    If you want to change the default value to be used in form resets, try this:

    $("#someId").attr("defaultValue", "newValue");
    

    See DEMO.

提交回复
热议问题