Javascript 为什么可以';我似乎不能正确地实现这个函数吗?

Javascript 为什么可以';我似乎不能正确地实现这个函数吗?,javascript,html,function,Javascript,Html,Function,我在一个外部.js文件中有一些代码,如下所示: function creditConfirm (){ textboxVType = document.getElementById('textboxType'); textboxVName= document.getElementById('textboxName'); textboxVNumber = document.getElementById('textboxNumber'); textboxVCode = document.get

我在一个外部.js文件中有一些代码,如下所示:

function creditConfirm (){
 textboxVType = document.getElementById('textboxType');
 textboxVName= document.getElementById('textboxName');
 textboxVNumber = document.getElementById('textboxNumber');
 textboxVCode = document.getElementById('textboxCode');
 textboxVAmount = document.getElementById('textboxAmount');

 if (textboxVType && textboxVName && textboxVNumber && textboxVCode && textboxVAmount =! " "){
     alert("Accepted");
     //All items made null

 }
 else{
     alert("Try again");
 }
}
<p1> Credit card type: </p1>

<input type = "text" id "textboxType">

<h1> </h1>

<p1> Name: </p1>

<input type = "text" id "textboxName">

<h1> </h1>

<p1> Number: </p1>

<input type = "text" id "textboxNumber">

<h1> </h1>

<p1> Security code: </p1>

<input type = "text" id "textboxCode">

<h1> </h1>

<p1> Donation amount: </p1>

<input type = "text" id "textboxAmount">

<button onclick="creditConfirm()">Confirm</button>  
然后我还有一些HTML代码,如下所示:

function creditConfirm (){
 textboxVType = document.getElementById('textboxType');
 textboxVName= document.getElementById('textboxName');
 textboxVNumber = document.getElementById('textboxNumber');
 textboxVCode = document.getElementById('textboxCode');
 textboxVAmount = document.getElementById('textboxAmount');

 if (textboxVType && textboxVName && textboxVNumber && textboxVCode && textboxVAmount =! " "){
     alert("Accepted");
     //All items made null

 }
 else{
     alert("Try again");
 }
}
<p1> Credit card type: </p1>

<input type = "text" id "textboxType">

<h1> </h1>

<p1> Name: </p1>

<input type = "text" id "textboxName">

<h1> </h1>

<p1> Number: </p1>

<input type = "text" id "textboxNumber">

<h1> </h1>

<p1> Security code: </p1>

<input type = "text" id "textboxCode">

<h1> </h1>

<p1> Donation amount: </p1>

<input type = "text" id "textboxAmount">

<button onclick="creditConfirm()">Confirm</button>  
信用卡类型:
姓名:
编号:
安全代码:
捐款额:
证实

我要做的是,如果所有的项目都填写好以打印第一个文本,如果缺少一个项目,则打印第二个文本并允许他们重试。然而,当我进入网站时,要么填写所有的框,要么留下一个未填写的框,然后单击确认按钮,什么都没有发生。我的JavaScript水平很低,我们的老师似乎拒绝教我们,所以我可能错过了一个非常明显的错误,有人能发现任何可能导致此不起作用的东西吗

如果语句正确,您没有检查
中的元素的值。

在具有
&&
(或
|
)条件的
if
语句中,每个条件都必须完整且独立

此外,要检查表单字段中的数据,必须检查其
属性

你还有
=而不是
=

if(textboxVType.value !="" && 
   textboxVName.value != "" && 
   textboxVNumber.value !="" && 
   textboxVCode.value !="" && 
   textboxVAmount.value != "") {}

您没有正确检查
if
语句中的元素值。

在具有
&&
(或
|
)条件的
if
语句中,每个条件都必须完整且独立

此外,要检查表单字段中的数据,必须检查其
属性

你还有
=而不是
=

if(textboxVType.value !="" && 
   textboxVName.value != "" && 
   textboxVNumber.value !="" && 
   textboxVCode.value !="" && 
   textboxVAmount.value != "") {}

您要检查dom元素是否真实(只要您在html中声明它们,这将始终是真实的),而不是检查它们是否有值集

将您的if更改为

if (textboxVType.value && textboxVName.value && textboxVNumber.value 
   && textboxVCode.value && textboxVAmount.value){
     alert("Accepted");
 }

您要检查dom元素是否真实(只要您在html中声明它们,这将始终是真实的),而不是检查它们是否有值集

将您的if更改为

if (textboxVType.value && textboxVName.value && textboxVNumber.value 
   && textboxVCode.value && textboxVAmount.value){
     alert("Accepted");
 }
1) 操作员
=不存在<代码>=可以

2)
textboxVType
textboxVName
textboxVNumber
textboxVCode
textboxVAmount=!“
是4个不同的条件。你不能这样分解条件。相反,您必须以这种方式编写
textboxVType.value!=“”&&textboxVName.value!=“”&&textboxVNumber.value!=“”&&textboxVCode.value!=“”&&textboxVAmount.value!=“”
用于访问您获得的DOM元素的值

3) 如果要检查文本框是否为空,请使用
!=“
而不是
!”“”
(仅检查文本框是否仅包含空格)

1)运算符
=不存在<代码>=可以

2)
textboxVType
textboxVName
textboxVNumber
textboxVCode
textboxVAmount=!“
是4个不同的条件。你不能这样分解条件。相反,您必须以这种方式编写
textboxVType.value!=“”&&textboxVName.value!=“”&&textboxVNumber.value!=“”&&textboxVCode.value!=“”&&textboxVAmount.value!=“”
用于访问您获得的DOM元素的值


3) 如果要检查文本框是否为空,请使用
!=“
而不是
!”“”
(这只会检查文本框是否只包含空格)

虽然我很感激您刚刚开始使用JS,而且这可能略高于您的舒适区,但您可能对一种快捷方式感兴趣,可以分别记录和检查输入ID的所有值:

在这里,我们使用提取所有输入,然后使用检查每个输入的值。如果其中任何一个为空,则会发出接受警报,否则重试

function creditConfirm() {
  const inputs = document.querySelectorAll('input');
  const emptyField = [...inputs].some(input => input.value === '');
  if (!emptyField) {
    alert("Accepted");
  } else {
    alert("Try again");
  }
}

.

虽然我很感激您刚刚开始使用JS,这可能略高于您的舒适区,但您可能会对一种快捷方式感兴趣,可以分别记录和检查输入ID的所有值:

在这里,我们使用提取所有输入,然后使用检查每个输入的值。如果其中任何一个为空,则会发出接受警报,否则重试

function creditConfirm() {
  const inputs = document.querySelectorAll('input');
  const emptyField = [...inputs].some(input => input.value === '');
  if (!emptyField) {
    alert("Accepted");
  } else {
    alert("Try again");
  }
}

.

如果(textboxVType&&textboxVName&&textboxVNumber&&textboxVCode&&textboxVAmount=!“”)不是检查所有内容是否满足条件的正确方法。您必须逐个元素进行检查,例如:
if(txtType!=”&&txtType2!=”)
-还有它的
=
=-另外,您有HTMLElements,因此它们永远不会等于空字符串,您要检查
属性
如果(textboxVType&&textboxVName&&textboxVNumber&&textboxVCode&&textboxVAmount=!”)
不是检查所有内容是否满足条件的正确方法。您必须逐个元素进行检查,例如:
if(txtType!=”&&txtType2!=”)
-还有它的
=
=-另外,您有HTMLElements,因此它们永远不会等于空字符串,您要检查
属性您建议的代码检查DOM元素与空字符串的相等性。@ScottMarcus您是对的,我忘记了.value,谢谢。正在编辑。您建议的代码将检查DOM元素与空字符串的相等性。@ScottMarcus您是对的,我忘记了.value,谢谢。现在编辑。谢谢,这真的很有帮助!有没有办法用它清理所有的箱子?谢谢你的帮助谢谢,这真的很有帮助!有没有办法用它清理所有的箱子?谢谢你的帮助