如何向HTML元素添加属性?我尝试了以下但该属性没有更新。
$(document).ready(function() {
$('#username_input').attr('value', 'something')
console.log(getUrlVars()["username"]);
console.log($('#username_input'));
console.log($('#username_input').css("display"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="POST">
<input type="text" name="title" placeholder="Album title"></input>
<input id="username_input" type="text" name="username" style="display:none;" />
<button type="submit" name="add" class="btn btn-success">+ Add Album</button>
</form>
答案 0 :(得分:0)
您没有记录属性值,而是记录包含该元素的jQuery对象。变化
console.log($("#username_input"));
到
console.log($("#username_input").attr("value"));
答案 1 :(得分:0)
你有语法问题。只需添加“;”到第二行结束
$('#username_input')。attr('value','something');
$(document).ready(function() {
$('#username_input').attr('value', 'something');
console.log(getUrlVars()["username"]);
console.log($('#username_input'));
console.log($('#username_input').css("display"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="POST">
<input type="text" name="title" placeholder="Album title"></input>
<input id="username_input" type="text" name="username" style="display:none;" />
<button type="submit" name="add" class="btn btn-success">+ Add Album</button>
</form>