未捕获的TypeError:无法读取未定义的属性“推" [英] Uncaught TypeError: Cannot read property 'push' of undefined
问题描述
你好,我用jquery生成文本框的每个人都是通过jquery动态生成的.现在的问题是我想在ajax函数中获取文本框的值,然后ajax将此值传递给我提到的其他php页面.当我在文本框上键入文本时,它会提醒我单击提交按钮时输入的值,但它没有将此值传递给其他页面,因为它给出了错误提示:
Hello Everyone I have used jquery to generate a textbox.It is generated dynamically through jquery.Now the problem is i want to fetch the textbox value in ajax function and then ajax pass this value to other php page which i have mentioned.When i type a text on textbox it gives me alert the value which i have type when i click submit button but it did not pass this value to other page because it gives error :
-未被捕获的TypeError:无法读取未定义的属性"push".
-Uncaught TypeError: Cannot read property 'push' of undefined.
我的代码是:-
在第一个脚本中,我习惯于动态生成texbox,在第二个脚本中,我想获取文本框的值并将该值传递给php文件.
在第二个脚本中,我在此行中遇到上述错误
dynamic_text.push($(this).parents("tr").find(".dynamic_text").val())
My code is:-
In first script i used to generate texbox dynamically and in second script i want to fetch the value of textbox and pass that value to php file.
In second script i have above error in this line
dynamic_text.push($(this).parents("tr").find(".dynamic_text").val())
<script>
jQuery(function($){
$(".select_drop").on('change', function(){
var $this = $(this),
selectedType = $this.val();
if($this.closest('tr').find('.dynamic_text').length == 0) $(this).closest('tr').append("<table><tr><td><form method='post'><input type='text' class='dynamic_text'><td></form></td></tr></table>")
//if($this.closest('tr').find('.dynamic_text').length == 0) $(this).closest('tr').append("<form method='post'><td><input type='text' class='dynamic_text'></td></form>")
});
});
</script>
<script>
$(document).ready(function(){
$('#submit').click(function(){
<?php global $store; ?>
var store= '<?php echo $store; ?>';
var textbox = [];
var textbox1 = [];
var check = [];
//var dynamic_text= $('.dynamic_text').val();
var dynamic_text;
// alert(dynamic_text);
$('.check').each(function() {
if ($(this).is(':checked')) {
var current = $(this).val();
//alert(current);
textbox.push($(this).parents("tr").find(".textbox").val())
textbox1.push($(this).parents("tr").find(".textbox1").val())
dynamic_text.push($(this).parents("tr").find(".dynamic_text").val())
}
check.push($(this).val());
});
$.ajax({
url: 'aj2.php',
type: 'post',
data: { check: textbox, store: store, oldval: textbox1,dynamic_text:dynamic_text},
success:function(data){
alert(data);
//alert("Data save!!!");
}
});
return false;
});
});
</script>
突出显示错误
推荐答案
这是因为您没有为变量dynamic_text
设置值:
This is because you are not setting a value to the variable dynamic_text
:
var dynamic_text;
因此,其值将为undefined
.显然,undefined
没有.push()
方法.那就是错误的意思:
As such it's value will be undefined
. And clearly undefined
doesn't have a .push()
method; that's what the error is saying:
无法读取未定义的属性"push"
Cannot read property 'push' of undefined
您将需要将变量定义为数组:
You will want to define your variable as an array:
var dynamic_text = [];
这篇关于未捕获的TypeError:无法读取未定义的属性“推"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!