Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

javascript - 如何在jquery中创建数组?(how do I create an array in jquery?)

$(document).ready(function() {
  $("a").click(function() {
    $("#results").load("jquery-routing.php", 
       { pageNo: $(this).text(), sortBy: $("#sortBy").val()} 
    );
    return false;
  });
}); 

How do I create an array in jQuery and use that array instead of { pageNo: $(this).text(), sortBy: $("#sortBy").val()}(如何在jQuery中创建一个数组并使用该数组而不是{ pageNo: $(this).text(), sortBy: $("#sortBy").val()})

  ask by vick translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Some thoughts:(一些想法:)

  • jQuery is a JavaScript library, not a language.(jQuery是一个JavaScript库,而不是一种语言。)

    So, JavaScript arrays look something like this:(所以,JavaScript数组看起来像这样:)
     var someNumbers = [1, 2, 3, 4, 5]; 
  • { pageNo: $(this).text(), sortBy: $("#sortBy").val()} is a map of key to value.({ pageNo: $(this).text(), sortBy: $("#sortBy").val()}是键值的映射。)

    If you want an array of the keys or values, you can do something like this:(如果你想要一个键或值的数组,你可以这样做:)
     var keys = []; var values = []; var object = { pageNo: $(this).text(), sortBy: $("#sortBy").val()}; $.each(object, function(key, value) { keys.push(key); values.push(value); }); 
  • objects in JavaScript are incredibly flexible.(JavaScript中的对象非常灵活。)

    If you want to create an object {foo: 1} , all of the following work:(如果要创建对象{foo: 1} ,则以下所有工作:)
     var obj = {foo: 1}; var obj = {}; obj['foo'] = 1; var obj = {}; obj.foo = 1; 

To wrap up, do you want this?(总结一下,你想要这个吗?)

var data = {};
// either way of changing data will work:
data.pageNo = $(this).text();
data['sortBy'] = $("#sortBy").val();

$("#results").load("jquery-routing.php", data);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...