次のようなjsonオブジェクトがあります。
{"age":"18","50000": "641","100000": "1011","150000": "1391","gender":"female"},
{"age":"18","50000": "642","100000": "1012","150000": "1392","gender":"male"},
{"age":"20","50000": "643","100000": "1013","150000": "1393","gender":"male"}
ユーザー入力に基づいて価格を表示するための小さな「計算機」を構築しようとしています。
<label for="gender">Gender</label>
<select id="gender" name="gender">
<option value="Female">Female</option>
<option value="Male">Male</option>
</select>
<label for="amount">Amount</label>
<select id="amount" name="amount">
<option value="50000">500 00</option>
<option value="100000">1 00000</option>
<option value="150000">1 50000</option>
</select>
<label for="age">Age</label>
<input id="age" name="age" type="text" />
これは私が試したJavaScriptですが、フォームから正しい値をフィルタリングして表示する方法を理解できないようです。
$(document).ready(function () {
//JSON object
var obj = {"data":
[
{"age":"18","50000": "641","100000": "1011","150000": "1391","gender":"female"},
{"age":"18","50000": "642","100000": "1012","150000": "1392","gender":"male"},
{"age":"20","50000": "643","100000": "1013","150000": "1393","gender":"male"}
]
};
//Find the value when form is submitted
$('#target').submit(function() {
obj.data = jQuery.grep(obj.data, function(element, index){
return element.age && element.gender && element.amount; // retain appropriate elements
});
//Empty the div
$('#price-result').empty();
//Show the result in div
$("#price-result").append("Age: element.age, Gender: element.gender, Amount: element.amount, Price: element.price-from-json");
return false;//Stop page from reloading
});
});
どうすればこれを解決できるか、誰にもヒントがありますか?