首页 > 解决方案 > 在代码的 javascript 部分中传输表单的答案

问题描述

我正在开发移动和平板电脑应用程序,我选择使用本地推送通知 JavaScript API 并直接在我的头网站中添加部分代码以在我的应用程序中发送推送通知。这是JS代码的一部分:

<head> 

<script> 

var seconds = 60; //seconds from now on 

var message = “This is my push message !”; //the message 

var button = “Open App”; //the text of the button 

window.location.href = “sendlocalpushmsg://push.send?s=”+ seconds ”=msg!”message “&amp;!#” button +””; 

</script>

</head>

它适用于我的应用程序,但我无法使用我脑海中的这部分代码访问我的网站,因为我有一个 Safari 错误说:

“Safari cannot open “sendlocalpushmsg: //push.send? S = 120 = msg! It% 20is% 20Happy% 20Hour% 20now!% 20Check% 20it% 20out! &! # Open% 20App” because macOS does not recognize Internet addresses beginning with “sendlocalpushmsg:”.”

所以我决定在我的网站页面中创建一个表单,并使用提交按钮发送通知,以免简单地破坏对我网站的访问。

实际上我有这种形式,每个输入都会修改 JS 部分的内容:

<form>

<span>Time before to send the notif: </span>
<input id="time-push" type="text" placeholder="seconds from now on"><br>


<span>The push notification message: </span>
<input id="message-push" type="text" placeholder="the push message"><br>

<span>Text to open the notification: </span>
<input id="button-push" type="text" placeholder="the text of the button"><br>

<input class="notif-push" type="submit" value="Submit"><br>

</form>

目标是修改 JS 代码的每一部分,当我点击提交时,我想发送通知:

$('.notif-push').click(function(){
    var seconds = document.getElementById('time-push').value; //seconds from now on
    var message = document.getElementById('message-push').value; //the push message
    var button = document.getElementById('button-push').value;; //the text of the button
    window.location.href = "sendlocalpushmsg://push.send?s="+ seconds +"=msg!"+ message +"&!#"+ button +"";
});

如何将我的输入连接到我的 js 部分代码?我是否需要在我的表单中添加这样的内容->

<form action="sendlocalpushmsg://push.send?s=”+ seconds ”=msg!”message “&amp;!#” button +”&quot;>

或者我的提交按钮中的类似内容->

<input class="notif-push" type="submit" value="Submit" onClick="myFunction()">

然后->

function myFunction() {
     window.location.href="sendlocalpushmsg://push.send?s=”+ seconds ”=msg!”message “&amp;!#” button +”&quot;;
   }

我说的是这种融合:

保留此输入的答案->

<span>Time before to send the notif: </span>
<input type="text" placeholder="seconds from now on"><br>

并将其添加到这部分JS代码中->

var seconds = document.getElementById('time-push').value; //seconds from now on

保留此输入的答案->

<span>The push notification message: </span>
<input type="text" placeholder="the push message"><br>

并将其添加到这部分JS代码中->

var message = document.getElementById('message-push').value; //the push message

保留此输入的答案->

<span>Text to open the notification: </span>
<input type="text" placeholder="the text of the button"><br>

并将其添加到这部分JS代码中->

var button = document.getElementById('button-push').value;; //the text of the button

通过提交表单来保留此输入的答案 ->

<input class="notif-push" type="submit" value="Submit"><br>

并从这个js部分发送通知->

window.location.href = "sendlocalpushmsg://push.send?s="+ seconds +"=msg!"+ message +"&!#"+ button +"";

标签: javascripthtmljqueryformspush-notification

解决方案


推荐阅读