ユーザーがフォームを送信してローカルストレージに保存し、その後入力をhtmlテーブルに表示できるアプリがあります。ユーザーは自分のデータを更新および削除できます。私の質問は、フォームにはユーザーの地理位置情報も表示されるのですが、そのデータをローカル ストレージに保存できないということです。HTML ファイル内の地理位置情報データのコードは次のようになります。
<form id="formStorage">
<ul>
<li>
<label for="txtTitle">Title:</label>
<input type="text" id="txtTitle"/>
</li>
<li>
<label for="txtEntry">Entry:</label>
<textarea id="txtEntry"></textarea>
</li>
<li>
<input type="submit" value="Save" id="btnSave"/>
</li>
</ul>
<dl>
<dt>Geolocation Status</dt>
<dd id='status'>Your Coordinates</dd>
<dt>Latitude</dt>
<dd id='eLatitude'>NA</dd>
<dt>Longitude</dt>
<dd id='eLongitude'>NA</dd>
</dl>
</form>
<!-- Geolocation coordinates & error message handling -->
<script>
if (navigator.geolocation) {
navigator.geolocation.watchPosition(
function (pos) {
$("#status").text("OK");
$("#eLatitude").text(pos.coords.latitude);
$("#eLongitude").text(pos.coords.longitude);
},
function (err) {
$("#status").text("ERROR: "+ err.message);
}
)
}
else {
$("#status").text("Geolocation is not supported in this browser");
}
</script>
<table id="tblStorage">
</table>
テーブルタグを動的に設定する追加、編集、削除、およびリスト機能を実行するフォーム送信時に呼び出される JavaScript ファイルがあります。関数はフォーム入力変数に対して機能しますが、地理位置情報データ変数に対しては機能しません。複数回繰り返してみました。何が足りないのか教えてもらえますか?
$(function(){
var selected_index = -1; //Index of the selected list item
var tableEntries = localStorage.getItem("tableEntries");//Retrieve the stored data
tableEntries = JSON.parse(tableEntries); //Converts string to object
if(tableEntries == null) //If there is no data, initialize an empty array
tableEntries = [];
function Add(){
var writing = JSON.stringify({
Title : $("#txtTitle").val(),
Entry : $("#txtEntry").val().
Latitude : $("#eLatitude").val(),
Longitude: $("#eLongitude").val()
});
tableEntries.push(writing);
localStorage.setItem("tableEntries", JSON.stringify(tableEntries));
alert("The entry was saved.");
return true;
}
function Edit(){
tableEntries[selected_index] = JSON.stringify({
Title : $("#txtTitle").val(),
Entry : $("#txtEntry").val(),
Latitude : $("#eLatitude").val(),
Longitude: $("#eLongitude").val()
});
localStorage.setItem("tableEntries", JSON.stringify(tableEntries));
alert("The entry was edited.")
operation = "A"; //Return to default value
return true;
}
function List(){
$("#tblStorage").html("");
$("#tblStorage").html(
"<thead>"+
" <tr>"+
" <th></th>"+
" <th>Title</th>"+
" <th>Entry</th>"+
" <th>Latitude</th>"+
" <th>Longitude</th>"+
" </tr>"+
"</thead>"+
"<tbody>"+
"</tbody>"
);
for(var i in tableEntries){
var writ = JSON.parse(tableEntries[i]);
$("#tblStorage tbody").append("<tr>"+
" <td><img src='edit.png' alt='Edit"+i+"' class='btnEdit'/><img src='delete.png' alt='Delete"+i+"' class='btnDelete'/></td>" +
" <td>"+writ.Title+"</td>" +
" <td>"+writ.Entry+"</td>" +
" <td>"+writ.Latitude+"</td>" +
" <td>"+writ.Longitude+"</td>" +
"</tr>");
}
}