简体   繁体   English

如何检查文本框是否为空并显示弹出消息

[英]How to check if textbox is empty and display a popup message

I am writing some code for a website to alert a user if any parameter in the textbox submission were left blank, and for some reason, it is not working.我正在为网站编写一些代码,以在文本框提交中的任何参数留空时提醒用户,并且由于某种原因,它不起作用。 I've tried everything and I'm not sure if I am taking the right approach.我已经尝试了所有方法,但不确定我是否采用了正确的方法。 Here is my javascript code:这是我的 javascript 代码:

let assetInfo = {
    asset_tag_no: $(`#asset_tag_no${i}`).val(),
    manufacturer: $(`#manufacturer_serial_no${i}`).val(),
    descriptions: $(`#description${i}`).val(),
    costs: $(`#cost${i}`).val(),
    po_no: $(`#p.o._no${i}`).val(),
    department_division_head: $(`#department_division_head${i}`).val(),
            }
        $('#submit').click(function(assetInfo){
        if($(assetInfo).val() == ''){
            alert('Input can not be left blank');
            }
        });

And here is my HTML:这是我的 HTML:

<button type="button" class="btn btn-primary" id='submit'>SUBMIT</button>

Any tips?有小费吗?

Try checking the specific values on the assetInfo object rather than the object itself.尝试检查assetInfo object 的具体值,而不是 object 本身。

$('#submit').click(function(assetInfo){
  if (assetInfo.asset_tag_no == '' || assetInfo.manufacturer == '' || assetInfo.descriptions == '' || assetInfo.costs == '' || assetInfo.po_no == '' || assetInfo.department_division_head == '') {
    alert('Input can not be left blank');
  }
});

Alternatively, you could simply add the required attribute to the input elements and then the browser can natively alert the user when the input is missing.或者,您可以简单地将required属性添加到input元素,然后浏览器可以在输入丢失时以本机方式提醒用户。

<input type="text" name="asset_tag_no" required/>

When you use JQuery to get an element via $('.foo') , for example, it returns a hot object which is linked to the DOM.例如,当您使用 JQuery 通过$('.foo')获取元素时,它会返回链接到 DOM 的热 object。 However, when you call el.val() on a JQuery element, you get a primitive so it is no longer linked with the DOM's value for that element.但是,当您在 JQuery 元素上调用el.val()时,您会得到一个原语,因此它不再与该元素的 DOM 值相关联。 Therefore, you should adjust your assetInfo constant to just include the elements.因此,您应该调整assetInfo常量以仅包含元素。 Then, get the primitive values of the elements when you need it in the event listener.然后,在事件侦听器中需要时获取元素的原始值。 Here are your constants:这是你的常量:

let assetInfo = {
    asset_tag_no: $(`#asset_tag_no${i}`),
    manufacturer: $(`#manufacturer_serial_no${i}`),
    descriptions: $(`#description${i}`),
    costs: $(`#cost${i}`),
    po_no: $(`#p.o._no${i}`),
    department_division_head: $(`#department_division_head${i}`),
}

Now, you can get an array of the elements via calling Object.values on assetInfo .现在,您可以通过在assetInfo上调用Object.values来获取元素数组。 After that, we can use the Array.prototype.every method to check if the value of each element is not empty.之后,我们可以使用Array.prototype.every方法来检查每个元素的值是否不为空。 You should also remove the parameter from the callback function because you can just access it via its name.您还应该从回调 function 中删除该参数,因为您只能通过其名称访问它。

$('#submit').click(function() {
  const allValid = Object.values(assetInfo).every((el) => el.val() != "");

  if (!allValid) {
    alert('Input can not be left blank');
  }
});

If you are talking about textboxes, here is the solution:如果你在谈论文本框,这里是解决方案:

So in HTML, you can call Javascript Functions during events using parameters.所以在 HTML 中,你可以在事件期间使用参数调用 Javascript 函数。 The function you can use is called 'onblur', which is fired when an object in the window loses focus.您可以使用的 function 称为“onblur”,它会在 window 中的 object 失去焦点时触发。 So you would type:所以你会输入:

<input type="text" onblur="myFunction(this)" />
<p id="errorMessage" />

And then we actually need the function for obvious reasons.然后出于显而易见的原因,我们实际上需要 function。 Now we would go:现在我们将 go:

const errorMessage = document.getElementById('errorMessage')
function myFunction(field) {
   if (field === "") {
      errorMessage.innerText = "One or more fields were not entered.";
   }
}

Hope this helped.希望这有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM