joshua.kim joshua.kim - 29 days ago 6
jQuery Question

how to use javascript data append from jsp servlet data

some Spring put an jsp file

this jsp file include javascript file

javascript file has like that

var data=${get some data from java}
var input =$(input[name=getdata]).attr("value",data);

$('#objForm').append(input);


and i want append this data Spring modelandView controlled called
setViewName("file/call/jsp")


and this jsp file get a data by same name input element like this

<input name="getdata" val=()/>

<input name="getdata" val=()/>

<input name="getdata" val=()/>

<input name="getdata" val=()/>


i wrote like this

<form id="objForm" action="" >
<input type="text" name="getdata" value=""/>
<input name="getdata" val=()/>

<input name="getdata" val=()/>

<input name="getdata" val=()/>

<input name="getdata" val=()/>


</form>


how to change my javascript code..

this script didn't append input element.

Answer

I am not sure exactly what you are looking for but how about this?

var data = [{
  "name": "Data1",
  "value": "THIS IS SOME DATA"
}, {
  "name": "Data2",
  "value": "THIS IS SOME OTHER DATA"
}, {
  "name": "Data3",
  "value": "more... 4566"
}];

var x = data[0].firstName;

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

  var input = "[" + i + "] <input type='text' name='" + data[i].name + "' value='" + data[i].value + "'><br>";

  $('#objForm').append(input);
}
input {
  width: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<form id='objForm'>
  <input type="text" name="getdata" val="">
  <br>
</form>

Comments