感谢ph1409,我找到了这个解决方案https://jsfiddle.net/poojjaval/a9u4mkwy/7 但是我一直在尝试使此脚本与PHP和MySQLi一起使用,例如,设置数据库的性别和体重范围的类型并将其加载到此脚本中,但是我似乎没有任何操作。谁能指出我正确的方向?
function Ticket(gender, weight) {
this.gender = gender;
this.weight = weight;
}
Ticket.prototype.getPrice=function() {
var price;
if(this.gender === "male" && this.weight > 200){
price=20;
}
else if(this.gender === "female" && this.weight > 200) {
price=15;
}
else if(this.gender === "male" && this.weight <= 200) {
price=10;
}
else if(this.gender === "female" && this.weight <= 200) {
price=7;
}
return price;
}
$("select").change(function() {
var genderOptions = [];
$("select option:selected").each(function() {
genderOptions.push($(this).text() + " ");
});
var newTicket = new Ticket($("#gender").val(),$('#weight').val());
var multiplyVal = newTicket.weight * newTicket.getPrice();
$("#yourTicket").text("Gender: " + newTicket.gender + " Weight: " +
newTicket.weight + " $" + newTicket.getPrice().toFixed(2) + " and your
multiplied value is " + multiplyVal);
});