gpt4 book ai didi

javascript - 使用多种表单选择过滤 json 对象

转载 作者:行者123 更新时间:2023-11-30 18:09:45 26 4
gpt4 key购买 nike

我有一个像这样的 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

});


});

有人知道如何解决这个问题吗?

最佳答案

obj.data中没有element.amount。年龄和性别在那里。

我正在遍历数据对象,检查是否有任何值匹配。我假设这些将是唯一记录,否则金额将被覆盖。

查看此 fiddle

$(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
$('#calc').click(function(e) {
e.preventDefault();

var data = jQuery.grep(obj.data, function(element, index){
return element.age && element.gender; // retain appropriate elements

});

var selectedGender = $('#gender').val().toString().toLowerCase();
var selectedAmount= $('#amount').val().toString().toLowerCase();
var selectedAge = $('#age').val().toString().toLowerCase();

var amount = "";

$.each(data,function(k, v){

if( data[k].age.toString().toLowerCase() == selectedAge &&
data[k].gender.toString().toLowerCase() == selectedGender &&
data[k][selectedAmount] != undefined){

amount = data[k][selectedAmount];


}

});


//Empty the div
$('#price-result').empty();

//Show the result in div
var displayText = "Age: " + selectedAge + ", Gender: " + selectedGender + ", Amount: " + amount + ", Price: element.price-from-json";

$("#price-result").append(amount == "" ? "No Results" : displayText);

return false;//Stop page from reloading

});


});

关于javascript - 使用多种表单选择过滤 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14891535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com