将Javascript结果转换为div

将Javascript结果转换为div

问题1:我的意思是要创建与数组的行一样多的按钮。这样,仅出现一个按钮。

<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>


编辑我的代码的详细信息

最佳答案

.html替换,并且您的报价不匹配。但这并不重要-jQuery在操作DOM方面比在处理字符串方面更好。尝试:

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


Here's a demo.

关于javascript - 将Javascript结果转换为div,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13557829/

10-11 12:08