2

Weird, line one works fine but line 3 give me TypeError: dojo.byId(...).attr is not a function. There is hidden fields that hold all student pair that as <input type="hidden" id="_hidden_studentname_{somestudentid}" value="aStudentName">, here {somestudentid} only indicate it as student id variable. The purpose of this is try to get a student name by student id.

dojo.connect(dijit.byId('_studentId_id'), 'onChange', function (val) {
    var studentId=dijit.byId("_studentId_id").attr("value"); // line 1
    var id="_hidden_studentname_"+studentId;
    var studentName=dojo.byId(id).attr("value");             // line 3
    dojo.byId("_student_text").attr("value", studentName);
});

So dojo doesn't allow variable put in dojo.byId()? I am pretty sure the <input type="hidden"> with that id does exist....

4

1 回答 1

6

请注意您dijit.byId在第 1 行中的使用方式,但dojo.byId在第 3 行中使用。前者返回一个小部件(具有attr函数),而dojo.byId后者返回一个没有attr方法的 DOM 元素。

DOM 元素可以直接操作属性,因此您可以更新代码以使用something.value = 'some other value';.

dojo.connect(dijit.byId('_studentId_id'), 'onChange', function (val) {
    var studentId=dijit.byId("_studentId_id").attr("value"); // line 1
    var id="_hidden_studentname_"+studentId;
    var studentName=dojo.byId(id).value;             // line 3
    dojo.byId("_student_text").value =studentName;
});
于 2013-04-17T17:21:28.740 回答