如果输入无效,如何制作弹出窗口(警告框)?

时间:2017-05-27 14:19:00

标签: javascript jquery html css

基本上我试图做的是,当我点击按钮时,它会检查是否已写入某些内容,如果它还没有我想要一个警告框出现。这可能吗?以下是我到目前为止的任何提示? (#inputvalue是写入时值的id。我为进程创建了一个名为validateForm的新函数。这可以在Jquery中做吗?我说我写的更多是Javascript,因为那主要是我和我#39; m曾经...)。 //纳塔莉!

function validateForm() {
    var x = document.forms["#inputValue"].value;
    if ("#inputValue" == "") {
        alert("Name must be filled out");
        return false;
    }
}

4 个答案:

答案 0 :(得分:0)

如果您使用jQuery(因为您已标记了您的问题),您可以尝试这样的事情:

        <plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-java2ws-plugin</artifactId>
            <version>3.1.11</version>


            <dependencies>
                <dependency>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-rt-frontend-jaxws</artifactId>
                    <version>3.1.11</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.cxf</groupId>
                    <artifactId>cxf-rt-frontend-simple</artifactId>
                    <version>3.1.11</version>
                </dependency>
            </dependencies>


            <executions>
                <execution>

                    <!-- bind this plugin goal to the specific maven phase -->
                    <phase>process-classes</phase>
                    <configuration>

                        <className>org.apache.hello_world.Greeter</className>
                        <genWsdl>true</genWsdl>
                        <verbose>true</verbose>
                    </configuration>
                    <!-- the plugin specific goal -->
                    <goals>
                        <goal>java2ws</goal>
                    </goals>
                </execution>
            </executions>

        </plugin>

$(function(){

    function validateForm(){
        var ele = $("#inputValue");
        if(!ele.val()){
            alert("Name must be filled out")
        }
    }

    $("#btnId").on("click", validateForm);
});
$(function(){
    function validateForm(){
        var ele = $("#inputValue");
        if(!ele.val()){
            alert("Name must be filled out")
        }
    }
    
    $("#btnId").on("click", validateForm);
 });

答案 1 :(得分:0)

当然,使用您提供的代码,您只需要正确定位#inputValue并在测试中使用该值。

function validateForm() {
    var x = document.getElementById('inputValue').value;
    if (x.trim() == "") {
        alert("Name must be filled out");
        return false;
    }
}
<form onsubmit="validateForm()">
  <input id="inputValue" type="text">
  <input type="submit">
</form>

使用jquery,它基本上是一回事。这是一个使用jquery中的提交处理程序而不是html中的onsubmit属性,并使用jquery选择器的示例。

$('form').on('submit',function(e) {
  var x = $('#inputValue').val();
  if (x.trim() == "") {
        alert("Name must be filled out");
        return false;
    }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input id="inputValue" type="text">
  <input type="submit">
</form>

答案 2 :(得分:0)

除了Michael Coker所说的,如果你通过脚本将事件监听器附加到表单标签会更好:

document.addEventListener("DOMContentLoaded", function() {
    document.getElementsByName("form")[0].addEventListener("submit", function() {
        var value = document.getElementById("inputValue").value;
        if (value == "")
        {
            window.alert("Name must be filled out!");
        }
    });
});

答案 3 :(得分:0)

以下是使用jQuery的解决方案。与上面的一些答案不同,我没有使用表格。

//Listen for the click of the button
$(document).on("click", "#enterButton", function(e) {

//Get the value of the input box
  var F_Name = $('[name="FirstNameInput"]').val();
  
  //If it is empty, alert.
  if (!F_Name.trim()) {
    alert('Empty');
  }else{
    //DO SOMETHING WITH THE INPUT, AJAX?
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="FirstNameInput">
<button type="button" id='enterButton'>Enter</button>