在Jquery中使用数组

Here is a code:

<input type="button" id="array[1]" value="Value1" />
<input type="button" id="array[2]" value="Value2" /> 
<input type="button" id="array[3]" value="Value3" /> 

And I want to do something like that:

$('#array').click(function() {
    id = this.id;       
    $.ajax({
        here goes type, url, data and else
    });     
})  

I want to id add array's number. For example, if I click button where id is array[3] so id gets value of 3 in Jquery's function. Hope you got what I mean.

Get rid of the ID attributes, add a common class array, and give them all a name of array[].

Then use this in your jQuery;

$('input.array').click(function() {
    var id = $(this).val();      
    $.ajax({
        here goes type, url, data and else
    });     
})  

I would try add class on each array.

<input type="button" class='array' id="array[1]" value="Value1" />
<input type="button" class='array' id="array[2]" value="Value2" /> 
<input type="button" class='array' id="array[3]" value="Value3" /> 


$('.array').click(function() {
   var id = $(this).attr('id');       
    $.ajax({
        here goes type, url, data and else
    });     
})  

Hope it helps

Use a regexp to extract the number

var reId = /[(\d+)]/;
$('#array').click(function() {
    var id = reId.exec($(this).attr("id"))[0];       
    $.ajax({
        //id is now a string containing only the number
    });     
})  

A crude approach:

$(this).attr("id").replace(/[^0-9]*/ig, "")

Full example:

<!DOCTYPE html>
<html>
<head>
    <title>2939346</title>
    <script src="http://www.google.com/jsapi"></script>
    <script>google.load("jquery", "1");</script>
    <script type="text/javascript" charset="utf-8">
        $(document).ready(function(){
            $("input[type=button]").click(function(){
                alert($(this).attr("id").replace(/[^0-9]*/ig, ""));
            });
        });
    </script>
</head>
<body>
    <input type="button" id="array[1]" value="Value1" />
    <input type="button" id="array[2]" value="Value2" /> 
    <input type="button" id="array[3]" value="Value3" />
</body>
</html>

First of all you should not be using [] in id values ... only a-zA-Z0-9-_.:

look at the html ID attribute for more info on the specs..

you should name them something like id_1 and extract the number with regular expressions like the other answers suggest...
(remember that you cannot start the id value with a number only a-zA-Z)

if you're storing content in your elements i would use data-* to store them, For example instead of using arrays i would use a json string

<input type="button" class='array' data-json="{id:22,defualt_val:'Value1'}" value="Value1" />

then within your javascript code i would use!

$('input[data-json]').click(function(){
    $data = $('[data-json]',this)
    $.ajax({
        data:$data
    });     
}) 

This is a method used to store content to an element such as original-title etc.

I think i can help you solving this problem. As far as I understand input ids start with array keyword and contains original id within bracket which you wanna fetch and send on an ajax call. For elements that start with some predefined string you can use $("[name^=value]") jquery selector.

$("input[id^='array']").click(function() {
    var id = parseInt($(this).attr("id").split("[")[1].slice(0,-1));     
   //above variable should contain whatever you put inside brackets. 
    $.ajax({
        here goes type, url, data and else
    });     
});

That should give desired results. Cheers Ayaz Alavi