检查隐藏的表单输入在表单提交中具有价值 [英] Check Hidden Form Input has Value on Form Submit

查看:55
本文介绍了检查隐藏的表单输入在表单提交中具有价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有隐藏输入的表单:

I have a form with a hidden input:

<input type="hidden" name="productID" id="productID" value="">

我有一个隐藏的警报(为此使用Bootstrap):

I have a hidden Alert (using Bootstrap for this):

<div id="alert_ajax_error" class="alert alert-danger text-center" role="alert" style="display:none">
  You have not made a selection. Please select an Item and try again
</div>

<div class="text-center">
  <button type="submit" name="buttonType" value="saveSelected" class="btn btn-success">Submit</button>
</div>

当用户从表中选择时,隐藏的表单输入是通过另一个脚本填充的-它将productID添加为输入的值.

The hidden form input is populated via another script when users select from a table - it adds the productID as the value of the input.

我现在想添加一些客户端验证(已进行服务器端验证),以便如果用户单击提交",则通过检查隐藏输入的存在来检查他们是否做出选择,以查看是否它有一个值-如果为空,则将显示隐藏的警报:

I would now like to add some client side validation (have server side validation in place) so that if the user clicks Submit it checks to see if they have made a selection by checking for the presence of the hidden input to see if it has a value - if it is empty it will then show the hidden alert:

$("#alert_ajax_error").show();

我不确定是否可以使用Javascript/JQuery检查隐藏的输入,以及在可能的情况下如何解决此问题.

I'm not sure if it's possible to check a hidden input with Javascript/JQuery and how to go about this if it is possible.

推荐答案

基本上,您将需要使用if语句检查提交时的值

Basically you will want to check the value on submit using an if statement

$("form").submit(function(event) {

     event.preventDefault();

     if($('#productID').val() == '') {

          $("#alert_ajax_error").show();

          return false;

     }

     //.... more form actions if good ....

});

这篇关于检查隐藏的表单输入在表单提交中具有价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆