基本上,我下面有一个非常简单的代码。我需要更改金额并在有人更改时每次发送电子邮件。
更新: 感谢@ellipsis。除了发送数据外,您的版本几乎是理想的。 我需要将数据(html)从function()传输到document.write('\ x3Cscript type =“ text / javascript”'+ html +'> \ x3C / script>');
var amount = '';
var email = '';
var html='';
$(document).ready(function() {
$("#amount").keyup(function() {
amount = $('#amount').val();
email = $('#email').val();
var key = '123345';
var hash = '' //sha256(email+amount+key); // dynamic email & amount
html = 'src="test.html" '+'amount="' + amount + '" '+'email="' + email + '" '+'hash="' + hash + '" ';
$('.check').text('\x3Cscript type="text/javascript" '+html+'>\x3C/script>');
});
});
// I need to add the 'html' below
document.write('\x3Cscript type="text/javascript" '+html+'>\x3C/script>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<form action="test.html?" method="get">
<button id="button">Send it</button>
<input type="text" id="amount" >
<input type="text" id="email" >
<p class="check"></p>
</form>
答案 0 :(得分:0)
尝试一下
var amount = '';
var email = '';
var html='';
$(document).ready(function() {
$("#amount").keyup(function() {
amount = $('#amount').val();
email = $('#email').val();
var key = '123345';
var hash = '' //sha256(email+amount+key); // dynamic email & amount
html = 'src="test.html" '+'amount="' + amount + '" '+'email="' + email + '" '+'hash="' + hash + '" ';
$('.check').text('\x3Cscript type="text/javascript" '+html+'>\x3C/script>');
//$('.check').html(html);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<form action="test.html?" method="get">
<button id="button">Send it</button>
<input type="text" id="amount">
<input type="text" id="email">
<p class="check"></p>
</form>
答案 1 :(得分:0)
请勿在点击中声明amount
和email
vars
。这样,您将无法访问它们,而只能在click事件的功能范围内实例化它们。
尝试一下:
$(document).ready(function(){
$("#amount").click(function(){
amount = $('#amount').val(); // access the globally declared var amount
email = $('#email').val(); // access the globally declared var email
});
});
也不要使用全局变量。
编辑:
var amount = '';
var email = '';
var html='';
var key = '123345';
var hash = '' //sha256(email+amount+key); // dynamic email & amount
$(document).ready(function() {
$("#amount").keyup(function() {
amount = $('#amount').val();
});
$("#email").keyup(function(){
email = $('#email').val();
})
$('#button').click(function(){
setNewText();
})
});
function setNewText(){
var html = 'src="test.html" '+'amount="' + amount + '" '+'email="' + email + '" '+'hash="' + hash + '" ';
$('.check').text('\x3Cscript type="text/javascript" '+html+'>\x3C/script>');
document.write('\x3Cscript type="text/javascript" '+html+'>\x3C/script>');
}
您需要为email
和amount
设置2个单独的事件,因为否则您将仅跟踪输入更新之一。另外,我还创建了另一个函数来轻松更新文本,以防止两次编写相同的代码。您可以here尝试一下。
答案 2 :(得分:0)
您应该将所有变量都移到click事件函数中:
$(document).ready(function(){
$("#amount").click(function(){
var amount = $('#amount').val();
var email = $('#email').val();
var key = '123345';
var hash = sha256(email + amount + key); // dynamic email & amount
html = '';
html += 'tamount="' + amount + '" '; // dynamic
html += 'email="' + email + '" '; // dynamic
html += 'hash="' + hash + '" '; // dynamic every time
$('.check').append(html);
document.write('\x3Cscript type="text/javascript" '+html+'>\x3C/script>');
});
});
添加:
您的问题是您的变量在不同的范围内变化。
您的行$('.check').append(html);
将立即执行。但是此时,直到用户单击,才设置var amount
和所有其他变量。
答案 3 :(得分:0)
var amount = '';
var email ='';
$(document).ready(function(){
$("#amount").change(function(){
postRun();
});
$("#email").change(function(){
postRun();
});
});
function postRun(){
amount = $('#amount').val();
email = $('#email').val();
var key = '123345';
var hash = sha256(email+amount+key); // dynamic email & amount
html = '';
html +='tamount="'+amount+'" '; // dynamic
html +='email="'+email+'" '; // dynamic
html +='hash="'+hash+'" '; // dynamic every time
}