简体   繁体   English

单击按钮后为空,则不应提交输入字段

[英]Input field should not submit if empty after button click

I have a search field and user can only input 2 or more characters: 我有一个搜索字段,用户只能输入2个或更多字符:

<?php

use yii\helpers\Html;
use app\models\User;
use yii\helpers\ArrayHelper;
use kartik\form\ActiveForm;
use kartik\widgets\Select2;
use yii\helpers\Url;
$session = Yii::$app->session;

/* @var $this yii\web\View */
/* @var $model app\models\PayslipTemplateSearch */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="searchbox" style="padding: 0; margin-right: 0px;">  

    <?php $form = ActiveForm::begin([
        'action' => ['searchresults'],
        'method' => 'get',
        'id' => 'searchForm'
    ]); ?>

    <div class="input-group">
       <input type="text" class="form-control" name="keyword" pattern=".{2,}" title="Keyword should have a minimum of 2 characters" required>
       <span class="input-group-btn">
            <button class="btn btn-primary" type="button">&#x1f50d;</button>
       </span>
    </div>
    <span class="error">Enter at least two characters</span>

    <?php ActiveForm::end(); ?>

</div>

<style type="text/css">
.error {
  display: none;
  font: italic medium sans-serif;
  color: red;
}
input[pattern]:invalid ~ .error {
  display: block;
}
</style>

<script>
$(function(){
    $(".btn-primary").click( function(e){
        e.preventDefault();
        $('#searchForm').submit();
    });
    $('.dropdown-responsive').addClass('manageadmindd');
    $('.select2-bootstrap-append').addClass('ddcontainer');
})

// function showError(txt)
// {
//   var err = document.getElementById('error');
//   err.innerHTML = txt;
//   err.style.display = 'block';
// }
</script>

Now, if I hit the ENTER KEY, it will display this: 现在,如果我按Enter键,它将显示以下内容: 在此处输入图片说明

But, if I press that search button, it will redirect to the search results page with a PHP error because it doesn't receive any keyword. 但是,如果我按下该搜索按钮,它将因为错误而无法重定向到PHP错误的搜索结果页面。

So how do I prevent this from happening? 那么如何防止这种情况发生呢?

EDIT: 编辑:

I put all my HTML code above. 我把我所有的HTML代码放在上面。 I am using Yii2 php framework. 我正在使用Yii2 php框架。

$('#searchForm').submit();
Remove this code from your script. 从脚本中删除此代码。 Its triggering form submit. 其触发形式为提交。 Even tough you have validated input field with pattern, the script will trigger the form submit action. 即使您已经使用模式验证了输入字段,该脚本也将触发表单提交操作。

Also provide type='submit' Attribute to your search button. 还为您的搜索按钮提供type='submit'属性。

You're preventing the button's default behavior, and then manually calling the form's submit . 您要防止按钮的默认行为,然后手动调用表单的submit

Change your button to an input[type=submit] field, and remove the code that you add an event listener to it, and everything should work as you expected: 将您的按钮更改为input[type=submit]字段,并删除向其添加事件侦听器的代码,一切应按预期工作:

<input type="submit" class="btn btn-primary" value="&#x1f50d;">

And remove: 并删除:

$(".btn-primary").click(function(e) {
  e.preventDefault();
  $('#searchForm').submit();
});

Add the disabled attribute on your button, then use your own javascript function to test if the field has input. 在按钮上添加Disabled属性,然后使用您自己的javascript函数测试该字段是否输入。 If it does, remove the disabled attribute from the submit button. 如果是这样,请从“提交”按钮中删除禁用的属性。

$( '.my-input' ).on( 'keyup', function( e ) {
    var inputVal = $( this ).val();
    if( !!inputVal ) {
        $( '.my-submit' ).removeAttr( 'disabled ');
    } else {
        $( '.my-submit' ).attr( 'disabled', 'disabled' );

    }
});

http://codepen.io/the_ruther4d/pen/ZbXYOx http://codepen.io/the_ruther4d/pen/ZbXYOx

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

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