admin管理员组

文章数量:1427281

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....

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....

Share Improve this question edited Apr 17, 2013 at 17:18 Dreamer asked Apr 17, 2013 at 16:47 DreamerDreamer 7,55127 gold badges110 silver badges198 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Note how you were using dijit.byId in Line 1, but using dojo.byId in line 3. The former returns a widget (which has an attr function), wheras dojo.byId returns a DOM element, which does not have an attr method.

DOM elements can manipulate attributes directly, so you can update the code to use 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;
});

本文标签: javascriptDojo TypeError dojobyId()attr is not a functionStack Overflow