所以我试图让输入的值调用这些数组中的一个,但是我知道的唯一方法是值[0],值[1]然而这显然会拆分该值。我正常做LUA,还不习惯jQuery。
$(function(){
// User Pets
var John = ['Dog', 'Alive'];
var Emily = ['Cat', 'Alive'];
var Frank = ['Fish', 'Dead'];
var Greg = ['Dog', 'Alive'];
var Hannah = ['Camel', 'Dead'];
$('button').on('click', function() {
var value = $('#name').val()
console.log(value[0], value[1])
}); // Button
}); // Function
答案 0 :(得分:1)
您可以使用对象:
var people = {
John: ['Dog', 'Alive'],
Emily: ['Cat', 'Alive'],
Frank: ['Fish', 'Dead'],
Greg: ['Dog', 'Alive'],
Hannah: ['Camel', 'Dead']
}
$('button').on('click', function() {
var value = $('#name').val();
var person = people[value];
if (!person) return console.log('User not found!');
console.log(person[0], person[1]);
});