Javascript 使用onclick和onsubmit提交表单

Javascript 使用onclick和onsubmit提交表单,javascript,forms,function,onclick,onsubmit,Javascript,Forms,Function,Onclick,Onsubmit,我正在尝试为我的公司设置一个“点击聊天”系统。它需要一个表单,从用户那里获取一些信息。提交表单时,应该使用.js文件中的脚本打开一个新窗口 我试图添加一些验证,这导致了onclick和onsubmit函数。当表单在未进行验证的情况下被subitted时,它会使用BG.startChatWithIssueForm(this.form,true)打开一个新窗口函数。但是,出于某种原因,当我包含onsubmit进行验证时,onclick会完全忽略它 我已经尝试用issueform嵌套BG.startc

我正在尝试为我的公司设置一个“点击聊天”系统。它需要一个表单,从用户那里获取一些信息。提交表单时,应该使用.js文件中的脚本打开一个新窗口

我试图添加一些验证,这导致了onclick和onsubmit函数。当表单在未进行验证的情况下被subitted时,它会使用
BG.startChatWithIssueForm(this.form,true)打开一个新窗口函数。但是,出于某种原因,当我包含onsubmit进行验证时,onclick会完全忽略它

我已经尝试用issueform嵌套
BG.startchat(this.form,true)formValidator()
函数的不同位置执行code>函数,但它仍然会导致文件下载提示,而不是打开新窗口

不知道我做错了什么。我已经研究了好几个星期了,似乎什么都没想到。Javascript绝对不是我的强项,因此任何帮助都将不胜感激

请参阅下面的代码:

JS:

函数Bomgar(){
var_host=“”;
var_protoRe=/^(http | https):\/\/;
/*私人的*/
函数_createURL(参数,用于弹出){
var qStr=“”;
for(参数中的变量k){
qStr+=“&”+encodeURIComponent(k)+“=”+encodeURIComponent(参数[k]);
}
qStr=“popup=“+(forPopup?”1):“0”)+“&c2cjs=1”+qStr;
返回_host+“api/start_session.ns?”+qStr;
};
函数_openWindow(参数){
返回窗口。打开(_createURL(params,true),'clickToChat',工具栏=否,目录=否,状态=否,菜单栏=否,可调整大小=是,位置=否,滚动条=否');
};
函数_重定向窗口(参数){
window.location.href=_createURL(参数,false);
};
函数_startChat(参数,doFull){
var w=_openWindow(参数);
如果(w&&!w.closed){return;}
else if(doFull){u重定向窗口(params);return;}
};
具有surveyValues(surveyValues,fallbackToFullWindow)的函数\u StartChat{
surveyValues.issue_菜单='1';
_startChat(调查值、回退到FullWindow);
};
/*公开的*/
//设置应在其上启动单击聊天的公共站点主机名。
this.setSite=函数(siteHostname){
如果(!_protoRe.test(sitehostment)){sitehostment=“http://”+sitehostment;}
如果(siteHostname[siteHostname.length-1]!='/'){siteHostname+='/';}
_主机=站点主机名;
};
//使用会话键启动“点击聊天”会话,如果弹出窗口由于弹出窗口阻止程序而无法打开,可以选择返回到完整的浏览器窗口重定向。
this.startChatWithSessionKey=函数(sessionKey,fallbackToFullWindow){
var p={short_key:sessionKey};
_startChat(p,fallbackToFullWindow);
};
//使用会话键和外键启动“点击聊天”会话,如果弹出窗口由于弹出窗口阻止程序而无法打开,可以选择返回到完整的浏览器窗口重定向。
this.startChatWithSessionKeyAndExternalKey=函数(sessionKey、externalKey、fallbackToFullWindow){
var p={short_key:sessionKey,externalKey:externalKey};
_startChat(p,fallbackToFullWindow);
};
//仅使用问题id而不使用其他前端调查字段启动单击聊天会话。
this.startChatWithIssueId=函数(issueId,fallbackToFullWindow){
_具有SurveyValue({id:issueId},fallbackToFullWindow)的StartChat;
};
//通过传递整个前端调查表单元素,启动单击聊天会话。
//这将在表单上提交所有非按钮输入元素值。
//将忽略任何意外的测量字段名称。
this.startChatWithIssueForm=函数(formElement,fallbackToFullWindow){
var params={};
对于(var i=0;i
HTML代码:

BG.setSite(“https://************.com”);
函数formValidator(){
//快速引用我们的字段
var issueid=document.getElementById('issueid');
var username=document.getElementById('username');
var userid=document.getElementById('userid');
var issuedesc=document.getElementById('issuedesc');
//按表单中显示的顺序检查每个输入
如果(进行选择(issueid,“请选择问题”))
{
如果(notEmpty(用户名,“请输入您的姓名”))
{
if(isAlphanumeric(用户名,“仅代表姓名的数字和字母”))
{
如果(notEmpty(userid,“请输入您的用户ID”))
{
if(isAlphanumeric(userid,“仅用于用户ID的数字和字母”))
{
如果(notEmpty(issuedesc,“请键入问题描述”))
{  
}
}
}
}
}
}
}
//检查以确保用户选择了他们的问题
函数madeSelection(elem、helperMsg){
if(elem.selectedIndex==0){
警报(helperMsg);
elem.focus();//将焦点设置为此输入
返回false;
}否则{
返回true;
function Bomgar() {
    var _host = "";
    var _protoRe = /^(http|https):\/\//;
    
    /* private */
    function _createURL(params, forPopup) {
        var qStr = "";
        for (var k in params) {
           qStr += "&"+encodeURIComponent(k)+"="+encodeURIComponent(params[k]);
        }
        qStr = "popup="+(forPopup ? "1" : "0") + "&c2cjs=1" + qStr;

        return _host+"api/start_session.ns?"+qStr;
    };

    function _openWindow(params) {
        return window.open(_createURL(params, true), 'clickToChat', 'toolbar=no,directories=no,status=no,menubar=no,resizable=yes,location=no,scrollbars=no');
    };

    function _redirectWindow(params) {
        window.location.href = _createURL(params, false);
    };

    function _startChat(params, doFull) {
        var w = _openWindow(params);
        if (w && !w.closed) { return; }
        else if (doFull) { _redirectWindow(params); return; }
    };
           
    function _startChatWithSurveyValues(surveyValues, fallbackToFullWindow) {
        surveyValues.issue_menu = '1';
        _startChat(surveyValues, fallbackToFullWindow);
    };

    /* public */

   // Set the public site hostname that click to chat should be started on.
   this.setSite = function(siteHostname) {
        if (!_protoRe.test(siteHostname)) { siteHostname = "http://"+siteHostname; }
        if (siteHostname[siteHostname.length-1] != '/') { siteHostname += '/'; }
        _host = siteHostname;
    };

    // Start a click to chat session using a session key, optionally falling back to a full browser window redirect if the popup window fails to open due to popup blockers.
    this.startChatWithSessionKey = function(sessionKey, fallbackToFullWindow) {
        var p = {short_key: sessionKey};
        _startChat(p, fallbackToFullWindow);
    };
   
    // Start a click to chat session using a session key and external key, optionally falling back to a full browser window redirect if the popup window fails to open due to popup blockers.
    this.startChatWithSessionKeyAndExternalKey = function(sessionKey, externalKey, fallbackToFullWindow) {
        var p = {short_key: sessionKey, external_key: externalKey};
        _startChat(p, fallbackToFullWindow);
    };
   
    // Start a click to chat session using just an issue id and no other front end survey fields.
    this.startChatWithIssueId = function(issueId, fallbackToFullWindow) {
        _startChatWithSurveyValues({id: issueId}, fallbackToFullWindow);
    };

    // Start a click to chat session by passing the entire front end survey form element.
    // This will submit all non-button input element values on the form.
    // Any unexpected survey field names will be ignored.
    this.startChatWithIssueForm = function(formElement, fallbackToFullWindow) {
        var params = {};
        for (var i = 0; i < formElement.elements.length; i++) {
            var e = formElement.elements[i];
            if (e.name && e.value && e.type && e.type != 'button' && e.type != 'submit') {
                params[e.name] = e.value;
            }
        }
        formElement = undefined;
        params.issue_menu = '1';
        _startChat(params, fallbackToFullWindow);
        return false;
    };

    // Start a session with a representative id and name.
    this.startChatWithRepIdName = function(repId, repName, fallbackToFullWindow) {
        var p = {id: repId, name: repName};
        _startChat(p, fallbackToFullWindow);
    };

    return this;
}
var BG = Bomgar();
<script type="text/javascript" src="https://***.******.com/api/clicktochat.js"></script>
<script type="text/javascript">
BG.setSite("https://***.******.com");
</script>
<script type='text/javascript'>
function formValidator(){
    // Make quick references to our fields
    var issueid = document.getElementById('issueid');
    var username = document.getElementById('username');
    var userid = document.getElementById('userid');
    var issuedesc = document.getElementById('issuedesc');
   
    // Check each input in the order that it appears in the form

    if(madeSelection(issueid, "Please choose an issue"))
        {

        if(notEmpty(username, "Please enter your name"))
            {

            if(isAlphanumeric(username, "Numbers and Letters Only for name"))
                {

                if(notEmpty(userid, "Please enter your user ID"))
                    {

                    if(isAlphanumeric(userid, "Numbers and Letters Only for user ID"))
                        {

                        if(notEmpty(issuedesc, "Please type a description of your problem"))
                            {  
                            }
                        }
                    }
                }
            }
        }
    }

    //check to make sure user selected their issue
    function madeSelection(elem, helperMsg){
        if(elem.selectedIndex == 0 ){
            alert(helperMsg);
            elem.focus(); // set the focus to this input
            return false;
        }else{
            return true;
        }
    }

    //check to make sure user entered something in the particular field
    function notEmpty(elem, helperMsg){
        if(elem.value.length == 0){
            alert(helperMsg);
            elem.focus();
            return false;
        }else{
            return true;
        }
    }

    //check to make sure user only entered numeric characters
    function isNumeric(elem, helperMsg){
        var numericExpression = /^[0-9]+$/;
        if(elem.value.match(numericExpression)){
            return true;
        }else{
            alert(helperMsg);
            elem.focus();
            return false;
        }
    }

    //check to make sure user only entered alpha characters
    function isAlphabet(elem, helperMsg){
        var alphaExp = /^[a-zA-Z]+$/;
        if(elem.value.match(alphaExp)){
            return true;
        }else{
            alert(helperMsg);
            elem.focus();
            return false;
        }
    }

    //check to make sure user entered only alpha or numeric characters
    function isAlphanumeric(elem, helperMsg){
        var alphaExp = /^[0-9a-zA-Z]+$/;
        if(elem.value.match(alphaExp)){
            return true;
        }else{
            alert(helperMsg);
            elem.focus();
            return false;
        }
    }
</script>
<script type="text/javascript">

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
               
function handleEnter (field, event) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {
        var i;
        for (i = 0; i < field.form.elements.length; i++)
           if (field == field.form.elements[i])
               break;
        i = (i + 1) % field.form.elements.length;
        field.form.elements[i].focus();
        return false;
    }
    else
        return true;
} 
</script>
 
 
<form action="https://***.******.com/api/start_session.ns" onsubmit="return formValidator();"  method="get">
What issue are you having?
<select onkeypress="return handleEnter(this, event)" id="issueid" name="id">
<option value="">Choose</option>
<option value="1">I need help getting started</option>
<option value="2">I am receiving an error</option>
</select>
<br />
Your First and Last Name: <input onkeypress="return handleEnter(this, event)" type="text" id="username" name="customer_name" /><br />
Your User ID (ABC1234): <input onkeypress="return handleEnter(this, event)" type="text" id="userid" name="customer_id" /><br />
Describe Your Issue: <textarea onkeypress="return handleEnter(this, event)" id="issuedesc" name="customer_desc"></textarea><br />
<input onkeypress="return handleEnter(this, event)" type="hidden" name="issue_menu" value="1" />
<input onkeypress="return handleEnter(this, event)" type="submit" value="Submit" onclick="BG.startChatWithIssueForm(this.form, true); return false;" />
<br>
<input onkeypress="return handleEnter(this, event)" type="button" name="reset_form" value="Clear" onclick="this.form.reset();">
 
</form>
</body>
<input onkeypress="return handleEnter(this, event)" type="submit" value="Submit" onclick="BG.startChatWithIssueForm(this.form, true); return false;" />
<input onkeypress="return handleEnter(this, event)" type="button" value="Submit" onclick="BG.handleSubmit(this.form, true);" />
BG.handleSubmit = function(formElement, fallBackToFullWindow) {
    if (!formValidator())
        return;
    BG.startChatWithIssueForm(formElement, fallBackToFullWindow);
    formElement.submit();
    return false;
}
function formValidator(){
    // Make quick references to our fields
    var issueid = document.getElementById('issueid');
    var username = document.getElementById('username');
    var userid = document.getElementById('userid');
    var issuedesc = document.getElementById('issuedesc');

    var valid = true;

    // Check each input in the order that it appears in the form

    if(!madeSelection(issueid, "Please choose an issue"))
        valid = false;

    if(!notEmpty(username, "Please enter your name"))
        valid = false;

    if(!isAlphanumeric(username, "Numbers and Letters Only for name"))
        valid = false;

    if(!notEmpty(userid, "Please enter your user ID"))
        valid = false;

    if(!isAlphanumeric(userid, "Numbers and Letters Only for user ID"))
        valid = false;

    if(!notEmpty(issuedesc, "Please type a description of your problem"))
        valid = false;

    return valid;
}