验证不适用于检查空字段

时间:2017-12-28 13:01:18

标签: javascript jquery

验证无法检查空字段 单击“创建”按钮时,没有消息发出警报 并且控制台显然没有显示任何错误

我的Jquery代码:

var assign_report_form = $('#assign_report_form'); 

    $('#create').on('click', function(){

        var source_id  = $('#src_id option:selected').text();
        var username   = $('#username').val();
        var email      = $('#email').val();
        var query_name = $('#query_name').val();
        var channel    = $('#channel').val();
        var condition  = $('#condition').val();

        if((source_id =="")||(username == "")||(email == "")||(query_name == "")||(channel == "")||(condition == "")){

             $('#error_message').show().delay(5000).fadeOut();
        }

        else{

        // var data = {'source_id': source_id,
        //             'username':username,
        //             'email':email,
        //             'query_name':query_name,
        //             'channel': 'email',
        //             'condition': condition
        //             }
        // console.log('data',data)




        $.ajax({
            url:'/add_report/',
            type:'POST',
            data:{'source_id': source_id,
                    'username':username,
                    'email':email,
                    'query_name':query_name,
                    'channel': 'email',
                    'condition': condition
                    },
            success: function(res){
                if(res =='success')
                    // $('#Add_modal').modal('toggle');
                    // $('#success_message').show().delay(5000).fadeOut();
                //     $("input[type=text],input[type=email],select").val("");
                // }
                // else{
                //     $('#error_message').show().delay(5000).fadeOut();
                // }

                console.log('form saved..')
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(errorThrown)
            }

        })
    event.preventDefault();    
    }
  });
}); 

验证无法检查空字段 单击“创建”按钮时,没有消息发出警报 并且控制台显然没有显示任何错误

这是html代码:

<button type="button" data-toggle="modal" data-target="#Add_modal" class="btn btn-secondary" id="add_row"  style="margin-right: 10px;">Add</button>
   <!-- Add New Report Model -->
   <div class="modal fade" id="Add_modal" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
      <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
               <h3 class="modal-title" id="lineModalLabel">Assign New Report</h3>
            </div>
            <div class="modal-body">
               <!-- content goes here -->
               <div class="alert alert-warning" id="#error_message" style='display:none;'>
                <strong>Error!</strong>Please fill Empty fields
               </div>
               <form id="assign_report_form">
                  {% csrf_token %}
                  <!-- <input type="hidden" id="src_id" value="{{ source_id }}"/>    -->
                  <div class="form-group">
                     <label for="username">Source ID</label>
                     <select id="src_id" class="form-control" name="source_id">
                        {% for source in sources %}
                        <option value="{{source_id}}" selected>{{ source.id }}</option>
                        {% endfor %}
                     </select>
                  </div>
                  <div class="form-group">
                     <label for="username">Username:</label>
                     <input type="text" class="form-control" name="username" id="username" placeholder="Username">
                  </div>
                  <div class="form-group">
                     <label for="email">Email</label>
                     <input type="email" class="form-control" name="email" id="email" placeholder="Email">
                  </div>
                  <div class="form-group">
                     <label for="queryname">Query_Name</label>
                     <input type="text" class="form-control" name="query_name" id="query_name" placeholder="Query_Name">
                  </div>
                  <div class="form-group">
                     <label for="channel">Channel</label>
                     <input type="text" class="form-control" name="channel" id="channel" placeholder="Channel">
                  </div>
                  <div class="form-group">
                     <label for="condition">Condition</label>
                     <input type="text" class="form-control" name="condition" id="condition" placeholder="Condition">
                  </div>
                  <input  type="button" id="create" class="btn btn-primary" value='Create'/>
               </form>
            </div>
         </div>
      </div>

2 个答案:

答案 0 :(得分:1)

在你的html代码中,你需要省略&#34;#&#34;在id =&#34;#error_message&#34;并写入id =&#34; error_message&#34;代替。

答案 1 :(得分:0)

您应始终使用ALTER PROCEDURE [dbo].[Return_Attribution] @Startdate datetime, @EndDate datetime, @Currency nvarchar(10) AS BEGIN DECLARE @portfolios AS NVARCHAR(MAX) DECLARE @Abfrage AS NVARCHAR(MAX) SET @portfolios = STUFF((SELECT '[' + PortfolioName + '], ' FROM tblPortfolio ORDER BY PortfolioName FOR XML PATH('')),1,5,NULL) SELECT @portfolios = LEFT(@portfolios,(LEN(@portfolios)-1)) SET @Abfrage ='SELECT Description, ' + @portfolios + ' FROM (SELECT tblReturnType.Description, tblPortfolio.PortfolioName, tblReturnAbs.Value FROM tblReturnType INNER JOIN ((tblReturnAbs INNER JOIN tblPortfolio ON tblReturnAbs.PortfolioNo = tblPortfolio.PortfolioNo) INNER JOIN tblInstrument ON tblReturnAbs.InstrumentNo =tblInstrument.InstrumentNo) ON (tblReturnType.ReturnType = tblReturnAbs.ReturnType) WHERE (tblReturnAbs.Date >= '+ @Startdate +' AND tblReturnAbs.Date <= Dateadd("d",-1,'+ @EndDate +')) AND tblInstrument.Currency = '+ @Currency +' ) AS x PIVOT ( sum(x.Value) For x.PortfolioName in (' + @portfolios + ') ) AS piv ORDER BY Description ' Execute (@Abfrage) END ,就像您的情况一样。

$('<dropdown selector>').val();

这将始终适用于var source_id = $('#src_id option').val(); 元素。