【问题标题】:How to display an alert message if the value is blank in the form如果表单中的值为空白,如何显示警报消息
【发布时间】:2020-05-05 08:54:38
【问题描述】:

我在脚本中有一个表单(引导程序),如果用户没有选择语言并且值“”为空白以显示警报消息并且他必须选择一种语言才能继续下一步,则需要它. (如您在表格中看到的空值:value“”代表“选择语言”)

我正在使用该表单进行谷歌小部件工具进行翻译,当访问者不点击任何内容并继续将文件保存到数据库中时,在数据库中我得到值:“选择语言”,我需要他们选择其中之一下拉菜单中的语言。

这是表单代码:

<div class="form-group">
  <label for="recipient-name" class="col-form-label">Language:</label>
  <input type="text" class="form-control langinput" id="country" value="" readonly>
  <select class="form-control langselect" aria-label="Language Translate Widget" id="country" style="display: none">
    <option value="">Select Language</option>
    <option value="English">English</option>
    <option value="Afrikaans">Afrikaans</option>
    <option value="Albanian">Albanian</option>
    <option value="Amharic">Amharic</option>
    <option value="Arabic">Arabic</option>

这里是保存文件到数据库的按钮代码:

<button style="margin-top: -5%" background-color="#357ae8" type="button" class="btn btn-success" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap" onclick="lang1()">Save to Database</button>

lang1 函数:

        var langPath = $("select.goog-te-combo option:selected").text();
        /*document.getElementById("country").options[document.getElementById("country").selectedIndex].text = langPath;
        document.getElementById("country").options[document.getElementById("country").selectedIndex].value = langPath;*/
        document.getElementById("country").value = langPath;
        //$(".langinput").value = langPath;
        //$('input[type="text"][class="langinput"]').prop("value", "langPath");
        //$('.langinput').text(langPath);
        $('.langinput').attr('value', langPath);
        //$(".langinput").display = "inline";

    }
    /*function stoppedTyping(){
        if($('#subtitle').val().length > 1 && $('#country').val().length >0 ) {
            document.getElementById('submit_button').disabled = false;
        } else {
            document.getElementById('submit_button').disabled = true;
        }
    }
    function stoppedSelect(){
        if($('#subtitle').val().length > 1 && $('#country').val().length >0 ) {
            document.getElementById('submit_button').disabled = false;
        } else {
            document.getElementById('submit_button').disabled = true;
        }
    }*/

【问题讨论】:

  • 它不是重复的,它与你提到我的问题的问题无关。量刑前请读到最后!
  • 其实是一样的。
  • 我不明白为什么它不适合你。可以插入lang1函数代码吗?
  • 能否请您编写您认为可以工作的代码我的朋友,我会尝试实现它

标签: php jquery html css bootstrap-4


【解决方案1】:

使用 bootstrap 只需将 required 添加到选择中:

<select class="form-control langselect" aria-label="Language Translate Widget" id="country" style="display: none" required>
    <option value="">Select Language</option>
    <option value="English">English</option>
    <option value="Afrikaans">Afrikaans</option>
    <option value="Albanian">Albanian</option>
    <option value="Amharic">Amharic</option>
    <option value="Arabic">Arabic</option>
</select>

编辑

将按钮 onclick 更改为 onsubmit:

<button style="margin-top: -5%" background-color="#357ae8" type="button" class="btn btn-success" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap" onsubmit="lang1()">Save to Database</button>

【讨论】:

  • 尝试将按钮 onclick="lang1()" 更改为 onsubmit="lang1()"
  • 我试过了,但是它保存了没有空语言值的文件,请查看下图:ibb.co/g6myZSw
【解决方案2】:

只是建议重写这个:&lt;option value=""&gt;Select Language&lt;/option&gt; 对此:&lt;option selected disabled&gt;Select Language&lt;/option&gt;

当用户点击下一个按钮时,检查选择的值长度是否为 0 并返回。

【讨论】:

  • 不起作用我的朋友,我正在使用该表单进行谷歌小部件工具进行翻译,当访问者不点击任何内容并继续将文件保存到数据库中时,我在数据库中得到值:“选择语言”,我需要他们从下拉菜单中选择一种语言。
  • 不要让用户保存文件,直到他们没有选择语言。我猜有一个保存按钮,所以检查它调用的函数中的值。
  • 好的,我的朋友我更新了问题,请看一下。
猜你喜欢
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多