网页打开时Javascript第一次不起作用?

时间:2018-08-01 20:02:57

标签: javascript html

我正在一个项目中,我需要将数据发送到表单的Firebase。

  

链接-http://ietvit.com/hackoff/

     

Github存储库链接-https://github.com/vivanks/hackoffproject

但是在填写表格时,它应该弹出警报窗口并提交表格,但是当我第一次打开网站时,它在刷新时并没有执行任何操作,而是开始完美运行,但不是第一次。任何原因或任何预防建议。

示例:

我第一次打开我的网站并填写订阅表单,但它只是刷新页面而不是更新firebase和弹出警报窗口。

  

我的html表单-

<form class="contact100-form validate-form">
                        <div class="wrap-input100 m-b-10 validate-input" data-validate = "Name is required">
                            <input id="name" class="s2-txt1 placeholder0 input100" type="text" name="name" placeholder="Your Name">
                            <span class="focus-input100"></span>
                        </div>

                        <div class="wrap-input100 m-b-20 validate-input" data-validate = "Email is required: ex@abc.xyz">
                            <input id="email" class="s2-txt1 placeholder0 input100" type="text" name="email" placeholder="Email Address">
                            <span class="focus-input100"></span>
                        </div>

                        <div class="w-full">
                            <p class="s2-txt3 p-t-18">
                        And don’t worry, we hate spam too! You can unsubcribe at anytime.
                    </p><br>
                            <button id="submitBtn" onclick="submitDetail()" name="submit" class="flex-c-m s2-txt2 size4 bg1 bor1 hov1 trans-04">
                                Submit
                                </button>

                        </div>
                    </form>
  

我的Java脚本-

var nameText = document.getElementById("name");
var emailText = document.getElementById("email");
var submitBtn = document.getElementById("submitBtn");

const firebaseRef = firebase.database().ref();




function submitDetail(){

    var nText = nameText.value;
    var eText = emailText.value;

    firebaseRef.push().set({
        name : nText,
        email : eText
    });

    window.alert("Subscribed Successfully")

}

1 个答案:

答案 0 :(得分:0)

                                                                                                           

                    <div class="wrap-input100 m-b-20 validate-input" data-validate = "Email is required: ex@abc.xyz">
                        <input id="email" class="s2-txt1 placeholder0 input100" type="text" name="email" placeholder="Email Address">
                        <span class="focus-input100"></span>
                    </div>

                    <div class="w-full">
                        <p class="s2-txt3 p-t-18">
                    And don’t worry, we hate spam too! You can unsubcribe at anytime.
                </p><br>
                        <button id="submitBtn" onclick="submitDetail(event)" type="submit" name="submit" class="flex-c-m s2-txt2 size4 bg1 bor1 hov1 trans-04">
                            Submit
                            </button>

                    </div>
                </form>

在js中,以防止重新加载:

function submitDetail(ev){
ev.preventDefault();
var nText = nameText.value;
var eText = emailText.value;

firebaseRef.push().set({
    name : nText,
    email : eText
});

window.alert("Subscribed Successfully")

}