【问题标题】:if the field empty JQUERY如果字段为空 JQUERY
【发布时间】:2013-04-09 07:20:37
【问题描述】:

我用 HTML 和 PHP 编写了一个简单的页面。所以,在 PHP 之前我想用 Jquery 检查空字段,但我还没有学习 Jquery,所以如果有人帮助我,我将不胜感激。

<?php
if(isset($_POST[add]))
{
if(empty($_POST[name]) || empty($_POST[surname])) {echo 'All form fields are required';}
}
?>
<form action="" method=post>
<table border=0  cellspacing=10 cellpadding=5>
        <tr>
            <td>Name:</td>
            <td><input type="text" name="name" size="10" value=""></td>
        </tr>
        <tr>
            <td>Surname:</td>
            <td><input type="text" name="surname" size="10" value=""></td>
        </tr>
        <tr>
            <td colspan=2><input type="submit" name="add" value="Add"></td>
        </tr>
</table>
</form>

【问题讨论】:

  • 你有一个错字 - 你的 surname 输入字段的关闭丢失..

标签: php jquery if-statement show


【解决方案1】:

您可以通过添加jquery来添加验证。

 $('input[name="add"]').click(function() { 

          if($('input[name="name"]').val() == '' || $('input[name="surname"]').val() == '') {
             $('.error').html('');

              if($('input[name="name"]').val() == '') {           
               $('.error').append('<p>Please enter your name</p>');                
              }

              if ($('input[name="surname"]').val() == '') {
                  $('.error').append('<p>Please enter your surname</p>');
              }

              $('.error').show();

              return false;

          }

          return true;

      })

html代码

    <div class="error" style="display:none"></div>
<form action="" method=post>
<table border=0  cellspacing=10 cellpadding=5>
        <tr>
            <td>Name:</td>
            <td><input type="text" name="name" size="10" value=""></td>
        </tr>
        <tr>
            <td>Surname:</td>
            <td><input type="text" name="surname" size="10" value=""</td>
        </tr>
        <tr>
            <td colspan=2><input type="submit" name="add" value="Add"></td>
        </tr>
</table>
</form>

【讨论】:

  • @RashadAghayev,我已编辑代码以删除警报消息并在 html 中显示错误消息。
【解决方案2】:

在提交时添加一个 javascript 函数让我们说 validate() 并在其中添加这个 jquery 代码

function validate() {
    var name = $("#name").val();
    if (name == "") {
        alert('Name is required');
        return false;
    }
}

【讨论】:

  • 我不想使用警报,我的意思是最好在表格下显示文本
  • 然后在列中添加一个span标签并给它一个id,比如说nameerror。然后代替 alert('Name is required'); give $("#nameerror").html('名字是必填的');
【解决方案3】:
<input class="field-name" type="text" name="name" size="10" value="">

我在这个输入字段中添加了一个类,所以我可以使用 jquery 选择器

var name = $('.field-name').val();
if(name="") {
    console.log('name is empty');
    //your code here to do something when the field is empty
}

【讨论】:

【解决方案4】:

下面的代码可以给你一些想法。我建议阅读关于 jquery 的教程

http://www.w3schools.com/jquery/default.asp

   <html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<script>
$(document).ready(function(){
    $("#add").submit(function(){
        if ($('#name').length == ""){
            alert("ERROR in name!");
        }   
    }); 
}); 
</script>
<form action="" method=post>
<table border=0  cellspacing=10 cellpadding=5>
        <tr>
            <td>Name:</td>
            <td><input type="text" name="name" id="name" size="10" value=""></td>
        </tr>
        <tr>
            <td>Surname:</td>
            <td><input type="text" name="surname" id="surname" size="10" value=""</td>
        </tr>
        <tr>
            <td colspan=2><input type="submit" name="add"  id="add" value="Add"></td>
        </tr>
</table>
</form>
</html>

【讨论】:

    猜你喜欢
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多