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

后端 未结 4 1098
青春惊慌失措
青春惊慌失措 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

    This

    $("#someId").val("newValue")
    

    will change the value of the input element.

    This

    $("#someId").attr("value","newValue")
    

    will change the value of the value attribute.

    The second may help if you want to write the element. So, for example:

    $("#someId").val("newValue")
    $("#someId").attr("value","newValue")
    write($("#someId").parent())
    

    -will result in the new value being rendered, whereas

    $("#someId").val("newValue")
    write($("#someId").parent())
    

    -will just show the original value.

提交回复
热议问题