JQuery Submit.click事件失效求助:模态表单提交无响应
问题排查与修复方案
先给你点个赞,敢上手实践就是很棒的开始!咱们一步步梳理你代码里的问题,都是新手很容易踩的小坑,改完就能正常跑起来啦:
1. 变量重复声明导致的覆盖问题
你这段代码里连续用了4次let firstName,这会导致后面的变量直接覆盖前面的,最后firstName的值会变成注册号码,完全不是你想要的用户名字!
// 错误写法 let firstName = $('#first-name').val(); let lastName = $('#last-name').val(); let firstName = $('#email').val(); // 重复声明,覆盖了之前的firstName let firstName = $('#registration-number').val(); // 再次覆盖
改成不同变量名就好,如果后面两个字段暂时不需要传到聊天配置里,也可以先注释掉:
// 正确写法 let firstName = $('#first-name').val(); let lastName = $('#last-name').val(); let email = $('#email').val(); // 用email变量存储邮箱 let registrationNumber = $('#registration-number').val(); // 用registrationNumber存储注册码
2. 聊天初始化代码不在点击事件内,点击按钮完全没反应
你现在把ININ.webchat.create()和webchat.renderPopup()写在了点击事件外面,这意味着页面一加载就会执行这段代码,而不是点击提交按钮后才触发!
必须把这部分代码移到$('#confirmation-button').click()的函数内部,这样点击按钮时才会执行收集表单、隐藏模态框、打开聊天的逻辑:
$('#confirmation-button').click(function(){ // 1. 收集表单字段值 let firstName = $('#first-name').val(); let lastName = $('#last-name').val(); // 2. 隐藏模态框(替换成你实际的模态框ID,比如Bootstrap的#exampleModal) $('#your-modal-id').hide(); // 3. 定义聊天配置 let chatConfig = { "webchatAppUrl": "https://apps.mypurecloud.ie/webchat", "webchatServiceUrl": "https://realtime.mypurecloud.ie:443", "orgId": "8410", "orgName": "fjellinjenas", "queueName": "Chat", "logLevel": "DEBUG", "locale": "", "data": { "firstName": firstName, "lastName": lastName, "addressStreet": "", "addressCity": "", "addressPostalCode": "", "addressState": "", "phoneNumber": "" }, "companyLogo": { "width": 600, "height": 149, "url": "http://i65.tinypic.com/2hr1ytg.jpg" }, "companyLogoSmall": { "width": 25, "height": 25, "url": "http://i68.tinypic.com/2m3gto6.jpg" }, "agentAvatar": { "width": 462, "height": 462, "url": "http://i67.tinypic.com/1eqted.png" }, "welcomeMessage": "Du snakker med kundebehandler.", "cssClass": "webchat-frame", "css": { "width": "100%", "height": "100%", "display": "block", "left": "90%" // 这里去掉最后一个逗号,避免语法错误 } }; // 4. 初始化并渲染聊天弹窗 ININ.webchat.create(chatConfig, function(err, webchat) { if (err) { console.error(err); throw err; } webchat.renderPopup({ width: 400, height: 400, title: 'Chat' }); }); });
3. 缺少模态框隐藏的代码
你提到点击提交后要隐藏模态框,所以必须在点击事件里添加$('#your-modal-id').hide(),记得把#your-modal-id替换成你实际使用的模态框ID哦。
4. 对象属性末尾的逗号问题
你的css对象里最后一个属性left: "90%",后面多了个逗号,虽然大部分浏览器能兼容,但严格模式下会报错,建议删掉这个逗号,避免不必要的语法问题。
额外小提示
如果改完还是有问题,打开浏览器开发者工具(F12)看Console面板,里面会显示具体报错信息,比如模态框ID写错会提示找不到元素,变量问题会提示重复声明,这对新手排查问题超有用!
内容的提问来源于stack exchange,提问作者Srki
相关产品推荐
相关产品推荐

