<form action="">
<input type="text" name="x" value="">
<input type="text" name="y" value="">
<input type="button" value="giải" onclick="ptb2.giai(this.form)">
</form>
<script>
var ptb2= new Object();
ptb2.a= prompt("Hệ số a")
ptb2.b= prompt("Hệ số b")
ptb2.c= prompt("Hệ số c")
ptb2.giai= function(form){
var delta = ptb2.b* ptb2.b - 4 * ptb2.a *ptb2.c;
var x1,x2;
if(delta <0){
alert("Pt vô nghiệm")
}
if( delta ==0){
alert("Phương trình có nghiệm kép")
x1= -ptb2.b/2*ptb2.a;
x2= -ptb2.b/2*ptb2.a;
form.x.value=eval(x1);
form.y.value=eval(x2);
}
if(delta>2){
alert("Phương trình có 2 nghiệm phân biệt")
x1=(-ptb2.b-Math.sqrt(delta))/(2*ptb2.a);
x2=(-ptb2.b+Math.sqrt(delta))/(2*ptb2.a);
form.x.value=eval(x1);
form.y.value=eval(x2);
}
}
Xin chào các anh chị em có thắc mắc thế này. Khi function ptb2.giai(form) thì khi nhập 3 hệ số a,b,c thì sẽ giải được phương trình bậc 2 và in kết quả ra tại input có name là x,y
Nhưng khi em sửa ptb2.giai(input) hay một tên khác thì chương trình báo lỗi như thế này
<form action="">
<input type="text" name="x" value="">
<input type="text" name="y" value="">
<input type="button" value="giải" onclick="ptb2.giai(this.input)">
</form>
<script>
var ptb2= new Object();
ptb2.a= prompt("Hệ số a")
ptb2.b= prompt("Hệ số b")
ptb2.c= prompt("Hệ số c")
ptb2.giai= function(input){
var delta = ptb2.b* ptb2.b - 4 * ptb2.a *ptb2.c;
var x1,x2;
if(delta <0){
alert("Pt vô nghiệm")
}
if( delta ==0){
alert("Phương trình có nghiệm kép")
x1= -ptb2.b/2*ptb2.a;
x2= -ptb2.b/2*ptb2.a;
input.x.value=eval(x1);
input.y.value=eval(x2);
}
if(delta>2){
alert("Phương trình có 2 nghiệm phân biệt")
x1=(-ptb2.b-Math.sqrt(delta))/(2*ptb2.a);
x2=(-ptb2.b+Math.sqrt(delta))/(2*ptb2.a);
input.x.value=eval(x1);
input.y.value=eval(x2);
}
}
</script>
Cho em hỏi nguyên nhân tại sao bị lỗi
Uncaught TypeError: Cannot read property ‘x’ of undefined


83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?