Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中强制使用所需的文本框_Javascript_Required - Fatal编程技术网

如何在javascript中强制使用所需的文本框

如何在javascript中强制使用所需的文本框,javascript,required,Javascript,Required,我的聊天有问题。 实际上,我有一个具有所需值的文本输入。 当我点击发送时,它返回空的而不是停止发送。。。 你能帮我吗 <script> $("#submitmsg").click(function() { var clientmsg = $("#usermsg").val(); $.post("chat-post.php", { text: clientmsg }); $("#usermsg").at

我的聊天有问题。 实际上,我有一个具有所需值的文本输入。 当我点击发送时,它返回空的而不是停止发送。。。 你能帮我吗

<script>
    $("#submitmsg").click(function() {
        var clientmsg = $("#usermsg").val();
        $.post("chat-post.php", {
            text: clientmsg
        }); 

$("#usermsg").attr('required', true);

        loadLog;
        return false;
    });
</script>

$(“#submitsg”)。单击(函数(){
var clientmsg=$(“#usermsg”).val();
$.post(“chat post.php”{
文本:clientmsg
}); 
$(“#usermsg”).attr('required',true);
负荷记录;
返回false;
});

基本上,如果消息输入的值为空,则在发送post请求之前需要添加一个条件。请检查以下内容:

    <script>
        $("#submitmsg").click(function() {
            var clientmsg = $("#usermsg").val();

// This is the additional condition
            if(clientmsg != '' || clientmsg != null){

                $.post("chat-post.php", {
                    text: clientmsg
                }); 
            }
            else{
                alert('Please enter the message!');
            }

    $("#usermsg").attr('required', true);

            loadLog;
            return false;
        });
    </script>

$(“#submitsg”)。单击(函数(){
var clientmsg=$(“#usermsg”).val();
//这是附加条件
if(clientmsg!=''| | clientmsg!=null){
$.post(“chat post.php”{
文本:clientmsg
}); 
}
否则{
警报('请输入消息!');
}
$(“#usermsg”).attr('required',true);
负荷记录;
返回false;
});

如果(clientmsg.trim())可以像这样验证输入字符串。最好在html代码中添加
required=true

 <input type='text' required="true">

为了使所需属性正常工作,您需要将输入放在表单中

<form>
  <input required type="text" />
</form>

我认为没有必要在每次单击按钮时设置所需的属性。您只需要做一次,那么为什么不简单地在html中添加所需的属性呢

即使您不想将其添加到HTML中,也只需添加一次即可

<script>
  $("#usermsg").attr('required', true); // Add the attribute required to the input

  // Do the AJAX function after you submit the form.
  // Before submitting, it will have to be validated, so you don't even have to validate it in JS. Just native HTML5
      $("form").submit(function() {
        // do the rest here
      }
    </script>

$(“#usermsg”).attr('required',true);//将所需的属性添加到输入中
//提交表单后执行AJAX功能。
//在提交之前,必须对其进行验证,因此您甚至不必在JS中对其进行验证。只是本地的HTML5
$(“表格”)。提交(函数(){
//其余的在这里做
}

第一步是查看错误控制台并查看是否存在任何错误。例如,什么是
loadLog
?虽然此代码片段可以解决问题,但确实有助于提高您文章的质量。请记住,您将在将来为读者回答问题,而这些人可能不知道r谢谢你的代码建议。谢谢帕特里克,我已经在那里添加了解释。:)
<script>
  $("#usermsg").attr('required', true); // Add the attribute required to the input

  // Do the AJAX function after you submit the form.
  // Before submitting, it will have to be validated, so you don't even have to validate it in JS. Just native HTML5
      $("form").submit(function() {
        // do the rest here
      }
    </script>