0

This is My jquery function here i append html code inside the function. That's got an error

function onDivisionChange() {
  $.ajax({
    type : 'POST',
    dataType : 'json',
    url : '/Example/selectDivision.html',
    data : ({
        id : $('#division').val()
    }),
    success : function(responseData) {

        if (responseData != null) {

            $('#student').find('td').remove().end().append('').val('-1');
            $.each(responseData, function(index, item) {

                $('#student').append(   
                    $('<td></td>').val(item['id']).html(item['id']),

                    $('<td></td>').val(item['id']).html(
                         '<a href="edit.html?id=' + item['id'] + '" >Edit</a>'),
                                                       ^ 
                                                       |_ Here Error Occured

  Error is the  '+ item['id'] + ' this line takes as string, not the id value 

   in browser Link as  ->   edit.html?id=' + item['id'] + '

            Correct format is  edit.html?id= 41            


                    $('<br />').val(item['id']).html(item['']));

            });
        } 
    }

});

}

If you know about this error please share your answer

EDIT

The page

function onStudentDivisionChange() {
    $.ajax({
        type : 'POST',
        dataType : 'json',
        url : '/Example/selectDivision.html',
        data : ({
            id : $('#division').val()
        }),
        success : function(responseData) {

            if (responseData != null) {

                $('#student').find('td').remove().end().append(
                        '').val('-1');
                $.each(responseData, function(index, item) {

                    $('#student').append(
                            $('<td></td>').val(item['id']).html(
                                    item['id']),
                            $('<td></td>').val(item['id']).html(
                                    item['name']),
                            $('<td></td>').val(item['id']).html(
                                    item['fatherName']),
                            $('<td></td>').val(item['id']).html(
                                    item['motherName']),
                            $('<td></td>').val(item['id']).html(
                                    item['admissionNo']),
                            $('<td></td>').val(item['id']).html(
                                    item['phoneNo']),
                            $('<td></td>').val(item['id']).html(
                                    '<a href="edit.html?id=' + (item['id']) + '" >Edit</a>'));
                    alert("Id:"+item['id']);

                });
            } else {
                $('#student').find('td').remove().end().append(
                        '<td th:text="${student}"></td>').val('-1');
            }
        }

    });
}
17
  • 1
    Please specify which error. Also please insert console.log(JSON.stringify(item)) just before the error line, and report what it outputs. Commented Nov 29, 2013 at 6:58
  • @Amadan the error is type mismatch error because the ' + item['id'] + ' is take as string not the inside value i think it's because the wrong format of quarts the Error show in console is Field error in object 'student' on field 'id': rejected value [' item['id'] ']; codes [typeMismatch.student.id,typeMismatch.id,typeMismatch.long,typeMismatch] Commented Nov 29, 2013 at 7:01
  • What is item - an array I guess - of what? Commented Nov 29, 2013 at 7:02
  • @user2310289: If it was an array, item['id'] would be undefined. It should be an object. Commented Nov 29, 2013 at 7:03
  • 1
    That error message "Field error...." seems to be an error generated by Spring framework, and nothing to do with clientside - neither jQuery nor HTML. Commented Nov 29, 2013 at 7:06

2 Answers 2

1

Thanks for you update to your question.

try doing

success : function(responseData) {

$.each(responseData, function(index, item) {
   var id = this.id;
   alert(id);

or

   var id = $(this).attr('id');

see jquery each loop examples

Sign up to request clarification or add additional context in comments.

Comments

0

I guess you can try using parseInt() to convert it.

 $('<td></td>').val(item['id']).html(
                     '<a href="edit.html?id=' + parseInt(item['id']) + '" >Edit</a>')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.