无法选择具有相同名称的动态创建的第二个文本框的值

时间:2015-11-22 18:50:17

标签: javascript jquery ajax

我有一个页面,我通过迭代getMessages()方法中的循环创建一个动态文本框元素(newComment)框,所以我的循环创建两个文本框并在循环中调用addComment()方法并尝试获取addcomment()中第二个文本框的值。但我无法获得第二个文本框的值,因为它只在第一个文本框中选择值。 你能否提出我需要做些什么来纠正这个以及其他方法来解决这个问题。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>profile page</title>

<script src="/WebServices/js/jquery-2.1.1.min.js" type="text/javascript"><       </script>

function showProfile(){
    var profileName = sessionStorage.profileName;

    var url="rest/profiles/"+profileName;
    //alert(profile.profileName);

    $.ajax(
            {
                type:'GET',
                url:url, 
                dataType:'json',
                contentType: "application/json; charset=utf-8",
                success:function(data)
                {
                            $("#getprofile").append("<b>profileName:</b>"+data.profileName);
                            $("#getprofile").append("<b>firstName:</b>"+data.firstName);
                            $("#getprofile").append("<b>lastName:</b>"+data.lastName+ "<br/>");
                            //$("#getprofile").append("<br/>"+'<input type="submit"   value="'+data[i].profileName+'"onClick="getProfile();return false;">'+"<br/>");
                            $("#getprofile").append("<br/>"+"<a href='#' onclick='getMessages();'>My Messages</a>");
                            //<a href="url">link text</a>

                }
            }
    );

}   


function getMessages(){ 
    $.ajax(
            {
                type:'GET',
                url:'rest/messages', 
                dataType:'json',
                contentType: "application/json; charset=utf-8",
                success:function(data)
                {
                        for (var i=0;i<data.length;++i){
                        var responseData=JSON.stringify(data);
                        //alert(data.comment);

                            $("#authResponse").append("<br/>"+"<b>MessageId:</b>"+data[i].id);
                            $("#authResponse").append("<b>Message:</b>"+data[i].message);
                            $("#authResponse").append("<b>Author:</b>"+data[i].author+ "<br/>");
                            $("#authResponse").append("<br/>"+'<a href="#" onclick=getComment(\''+data[i].id+'\');>view Comments</a>'+"<br/>"+"<br/>");
                            $("#authResponse").append('<input type="text" name="newComment" id="newComment"/>');
                            $("#authResponse").append("<br/>"+'<input type="submit"   value="Add comment" onClick="addComment(\''+data[i].id+'\');return false;">'+"<br/>");


                        }
                }
            }

    );
}


function getComment(messageId){

    var getCommentURL="rest/messages/"+messageId+"/comments";
    alert(getCommentURL);
    $.ajax(

        {
            type:'GET',
            url:getCommentURL,
            dataType:'json',
            contentType:"application/json; charset=utf-8",
            success:function(data)
            {   
                if($.trim(data))
                {
                    for (var i=0;i<data.length;++i){
                        $("#getComments").append("<b>Id:</b>"+data[i].id);
                        $("#getComments").append("<b>Comment :</b>"+data[i].message);
                        $("#getComments").append("<b>Author :</b>"+data[i].author);
                    }
                }else{
                    alert("No comments are added for this message.!!")
                }
            },
            error: function (error) {
                  alert('error inside getComment method');
            }   
        }   

    );
}
function addComment(messageId){

    //var comment = $("input#newComment").val();
    //var comment= document.getElementById('newComment').value;
    alert("comment:"+comment);
    var postCommentURL="rest/messages/"+messageId+"/comments";
    var authRequest = {"message":comment,"author":"system"};

    $.ajax(
    {
            type:'POST',
            url:postCommentURL,
            data:JSON.stringify(authRequest),
            dataType:'json',
            contentType:"application/json; charset=utf-8",
            success:function(data)
            {       
                    for (var i=0;i<data.length;++i){
                        alert("yo");
                        $("#addcommentResult").append("comment has been added successfully.!!");
                        if(data[i].id!=null){
                            $("#addcommentResult").append("comment has been added successfully.!!");
                        }
                        else{
                            $("#addcommentResult").append("comment not added.!!");
                        }
                    }
            },
            error: function (error) {
                  alert('error inside addComment method');
            }   
        }   

    );

}   

My Profile<br/><br/>
<div id = getprofile></div>
<div id = authResponse></div>
<div id = getComments></div>
<div id = addcommentResult></div>

0 个答案:

没有答案