-1

Q1:我的观点是创建与数组行一样多的按钮。我在这里有一些错误:s

<script type="text/javascript">
var myArray = [];

$('#button').click(function(){

var value1 = $('#value1').val();
var value2 = $('#value1').val();
var value3 = $('#value1').val();
var newArray = []; 
var newArray[0] = value1;
var newArray[1] = value2;
var newArray[2] = value3;
myArray.push(newArray);

$("#save").append(
    $("<button>").click(function() {
        myFunction.apply(null, myArray);
    }).text("Click me!")
   );
   });

});

function myFunction(value1,value2,value3)
{
var jsonData = $.ajax({
url: "file.php?value1=" + value1 + "&value2=" + value2 + "&value3=" + value3
dataType: "json",
async: false
}).responseText;
(...)
}
//edited: problem maybe found. I said buttons dont do anything because of this.
OUTPUT: file.php?value1=paul,23,USA&value2=undefined&value3=undefined
//it seems that value1 gets all values :s
</script>


<div id ="save"></div>

我正在寻找一个返回类似这样的解决方案:

例如:

<!--<button onclick="myFunction(name,age,country)">Click me</button>-->
<button onclick="myFunction(paul,23,USA)">Click me</button>
<button onclick="myFunction(john,23,USA)">Click me</button>

用更多细节编辑了我的代码

4

2 回答 2

1

这是因为myArray' 的第一项是数组 ( newArray) 你应该只调用一个数组参数尝试调用下面的函数,它应该可以工作,

myFunction.apply(null, newArray);
//or
myFunction.apply(null, [value1,value2,value3]);
于 2012-11-26T14:33:22.050 回答
0

猜猜这有帮助:

    <div  id="save">
    </div>      
    <script type="text/javascript">
        function addButtons(myArray){
            for(i=0;i<myArray.length;i++)
            {
                var button = $('<button id="btn_'+i+'" onclick="myFunction(this);">Click me</button>')
                $(button).data('details',myArray[i]).appendTo("#save");
            }
        }
        function myFunction(element){
            alert($(element).data('details'));
        }
    </script>
于 2012-11-26T14:35:57.200 回答