document.getElementById( "id" ).value 正在工作,但 $('#id).val 不起作用



>我有一些文本类型的输入字段。我正在从JSON对象获取值,并尝试使用javascript/jquery动态填充这些值。 当我使用document.getElementById('id').value填充值时,它工作正常。但$('#id').val()给出了错误。

.HTML:

<div class="form-group">
<label > First Name</label>
<input type="text" class="form-control" id="txtFirstName"> 
</div>

JavaScript:

var UserID = 25;
function editUser(response)
{
var obj = response;
$.each(obj.userDataDTOList, function(index, item){
if(item.userId == UserID)
{
document.getElementById('txtFirstName').value = item.userFirstname;
//$('#txtFirstName').val() = item.userFirstname;
}
});
}

这是错误:

未捕获的引用错误:赋值中的左侧无效 在对象。(编辑用户.js:35( at Function.each (jquery.min.js:2(

.val()是一个jQuery方法,它将值作为要在元素中设置的参数。

尝试

$('#txtFirstName').val(item.userFirstname);

最新更新