我正在尝试发送两个cookie值,一个名为" cookie1",另一个名为" cookie2"在表单上提交。我曾使用下面的脚本捕获一个cookie并以表格形式发送。现在我已经添加了与第一个脚本相同的第二个脚本,只发送了cookie2。这是因为我在javascript中使用相同的变量名称,所以只有最后一个被保存或者是其他东西吗?任何帮助将不胜感激!
以下是代码:
<input type="hidden" name="SubID" value="">
<input type="hidden" name="addinfo" value="">
<script type="text/javascript">
function getCookie(name) {
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}
var myForm = document.querySelector('form');
function handleSubmit() {
var myHiddenInput = document.querySelector('[name=SubID]');
myHiddenInput.value = getCookie("cookie1");
}
myForm.addEventListener("submit", handleSubmit, false);
</script>
<script type="text/javascript">
function getCookie(name) {
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}
var myForm = document.querySelector('form');
function handleSubmit() {
var myHiddenInput = document.querySelector('[name=addinfo]');
myHiddenInput.value = getCookie("cookie2");
}
myForm.addEventListener("submit", handleSubmit, false);
</script>
答案 0 :(得分:0)
第二个<script>
完全覆盖了第一个<input type="hidden" name="SubID" value="">
<input type="hidden" name="addinfo" value="">
<script type="text/javascript">
function getCookie(name) {
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}
var myForm = document.querySelector('form');
function handleSubmit() {
var myHiddenInput = document.querySelector('[name=SubID]');
myHiddenInput.value = getCookie("cookie1");
var myHiddenInput = document.querySelector('[name=addinfo]');
myHiddenInput.value = getCookie("cookie2");
}
myForm.addEventListener("submit", handleSubmit, false);
</script>
。您不必每次都声明相同的函数和变量:
$('#btnAdd').click(function (e) {
var nextTab = $('#tabs li').size()+1;
// create the tab
$('<li><a href="#tab'+nextTab+'" data-toggle="tab">Tab '+nextTab+'</a></li>').appendTo('#tabs');
// create the tab content
$('<div class="tab-pane" id="tab'+nextTab+'">tab' +nextTab+' content</div>').appendTo('.tab-content');
// make the new tab active
$('#tabs a:last').tab('show');
});