我有一张桌子。当用户单击表格单元格时,jquery 会获取单元格内的子项(输入标签)的 id 属性和 name 属性。当我提醒各个变量时,它们会正确提醒。但是,当我将值返回到数组并提醒他们在脚本中遇到问题的其他位置时。例如,当我提醒另一个函数中的值时,我得到 [object HTMLTableCellElement]。
在 document.ready 之后,我已经在脚本顶部的任何函数之外声明了 id 和 name 变量。为什么无法将 id 和 name 的正确值获取到另一个匿名函数或函数中。
<script>
$(document).ready(function() {
var id = 0;
var name = 0;
var values2 = [];
//when a table cell is clicked
values2 = $('table tr td').click(function() {
//grab the id attribute of the table cell's child input tags that have a class of hidden
id = $(this).children("input[class='hidden']").attr("id");
alert( id ); //<----- alerts id properly (output for example is 25)
//grab the name attribute of the table cell's child input tags that have a class of hidden
name = $(this).children("input[class='hidden']").attr("name");
alert( name ); //<----- alerts id properly (output for example is firstinput)
return [id , name]; //<------ here is where I try to return both these values in to an array
// so that I can use the values else where in the script. However
// I am not able to use the values
});
$( "#nameForm" ).submit(function( event ) {
// Stop form from submitting normally
event.preventDefault();
alert(values2[0]); // <----alerts [objectHTMLtableCellElement]
//this should alert ID
var posting = $.post( "comments.php", $('#nameForm').serialize(), function(data) {
$('#result').html(data);
});
return false;
// $('#result').html(term);
});
});
</script>