我需要从输入中获取多边形的坐标:有一个输入,我输入多边形的顶点数。然后出现输入(每个顶点的坐标对)。并且有按钮,点击它必须将X坐标写入数组xCoords,并将Y - 写入yCoords。
我不明白为什么这段代码不起作用:
var apexCount, xCoord, yCoord;
$('.js-apexCount').keyup(function() {
$('.js-coordinates, .js-warning').remove(); //Remove fields for apex coordinates input if they exist
apexCount = +$('.js-apexCount').val();
if ( apexCount < 3 ) {
$('#figureInfo').append('<p class="js-warning">It is not polygon!</p>');
} else {
for ( var i = 1; i < apexCount + 1; i++ ) {
$('#figureInfo').append('<label class="js-coordinates"><input type="text" name="x' + i + '"><input type="text" name="y' + i + '">' + i +
' apex coordinates</label>'); //Add fields for apex coordinates input
};
$('#figureInfo').append('<button class="js-coordinates">Write down coordinates</button>')
};
function getCoordinates() {
$('[name ^= "x"]').each(function() {
for ( i = 0; i < apexCount; i++ ) {
xCoord[i] = $(this).val();
};
});
$('[name ^= "y"]').each(function() {
for ( i = 0; i < apexCount; i++ ) {
yCoord[i] = $(this).val();
};
});
};
$('button.js-coordinates').click(function() {
getCoordinates();
alert('' + xCoord + yCoord);
});