如何使用j Query将值附加到html div ID

时间:2015-06-11 10:43:33

标签: javascript jquery html css

我正在一个应用程序中工作,我必须从数据库中获取动态值,并且必须动态地将它们附加到我的html div。要更清楚我有4个标签也有4个div是,现在我需要做,我必须附加4个值,我将动态从数据库到4个div。我在这里发布我的代码到目前为止我做了什么,有人请帮助

这是我的html部分,我有4个div

<div id="primaryLabel">
<table id="personalInformation">
        <tr>                
            <th>Address 1<span style='color: #FF0000; display: inline;'>*</span></th>   
            <td><label for="address1" id="address1ForLabel" ><%=address1 %></label></td>
            <td><a href="#" onclick="open_new();"><img style="border:0;" src="<%=request.getContextPath()%>/images/edit.png" title="Click to edit"></a> </td>
        </tr> 
        <tr>                
            <th>Address 2<span style='color: #FF0000; display: inline;'>*</span></th>   
            <td><label for="address2" id="address2ForLabel"><%=address2 %></label></td>
        </tr>           
        <tr>
            <th>City<span style='color: #FF0000; display: inline;'>*</span></th>
            <td><label for="city" id="cityForLabel"><%=city %></label></td>
        </tr>
        <tr>
            <th>Primary Phone<span style='color: #FF0000; display: inline;'>*</span></th>
            <td><label for="primary phone" id="primaryPhoneForLabel"><%=primaryPhone %></label></td>
        </tr>       
</table>
</div>

这是我的javascript / jQuery结束,我得到四个值。现在我想每次动态追加它们。在这里发表我的代码

 jQuery.getJSON(url+"&address1="+address1+"&address2="+address2+"&country="+country+"&state="+state+"&city="+city+"&zip="+zip+"&skypeId="+skypeId+"&twitter="+twitter+"&primaryPhone="+primaryPhone+"&secondaryPhone="+secondaryPhone+"&type="+type, function(data) {

        $( "div.success" ).fadeIn( 300 ).delay( 2500 ).fadeOut( 400 );

        for(var z=0; z<data.applicationArray.length;z++){

            applicationArray = data.applicationArray[z].split("$$##$$##");

            address1 = applicationArray[0];
            address2 = applicationArray[1];
            city = applicationArray[2];
            primaryPhone = applicationArray[3];

        }
        alert(address1+":"+address2+":"+city+":"+primaryPhone);

address1,address2,city,primaryPhone是那些参数。我尝试过的是什么

        $('#address1ForLabel').html(address1);
        $('#address1ForLabe2').append(address2);
        $('#cityForLabel').html(city);
        $('#primaryPhoneForLabel').html(primaryPhone);

但这对我不起作用。请有人帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

您可以尝试在额外的var lastCurrentTime = 0; $('#match').on('seeking',function(event) { var difference = 0; var newCurrentTime = $('#match').get(0).currentTime; if(newCurrentTime > lastCurrentTime) { difference = newCurrentTime - lastCurrentTime; playing = false; mtimer = mtimer + difference; $('#starttimer').html(getHHMMSS(mtimer)); playing = true; }else { difference = lastCurrentTime - newCurrentTime; playing = false; mtimer = mtimer - difference; console.log("Difference : " + difference); playing = true; if(mtimer <= 0) { mtimer = 0; $('#starttimer').html(getHHMMSS(mtimer)); } } lastCurrentTime = newCurrentTime; });

中附加您的值
[[ErrorType:class org.springframework.dao.InvalidDataAccessApiUsageException],
 [ErrorMessage:The given object has a null identifier: com.test.model.Test;
    nested exception is org.hibernate.TransientObjectException:
    The given object has a null identifier: com.test.model.Test]]

或替换原来的<div>

$('#address1ForLabel').append('<div>'address1'</div>');

答案 1 :(得分:0)

解决方案是

        $('#address1ForLabel').text(address1);
        $('#address2ForLabel').text(address2);
        $('#cityForLabel').text(city);
        $('#primaryPhoneForLabel').text(primaryPhone);