Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将变量从Javascript发送到Javascript_Javascript_Variables_Counter - Fatal编程技术网

将变量从Javascript发送到Javascript

将变量从Javascript发送到Javascript,javascript,variables,counter,Javascript,Variables,Counter,我需要将一个变量从一个脚本发送到另一个脚本,希望您能帮助我 我要发送的变量是“campos”,它是一个计数器 这是第一个脚本: <script> var campos = 0; function agregarCampophp(hola){ // alert(campos); var data = {}; data.pruebaId = $('#prueba').val(); data.deno = $('#subespecifica-descripcion').val

我需要将一个变量从一个脚本发送到另一个脚本,希望您能帮助我

我要发送的变量是“campos”,它是一个计数器

这是第一个脚本:

<script>

var campos = 0;

function agregarCampophp(hola){

// alert(campos);

 var data = {};
 data.pruebaId = $('#prueba').val();
 data.deno = $('#subespecifica-descripcion').val();
 data.prep = $('#partidaasociada-presupuestop').val();
 data.tot = $('#partidaasociada-totalp').val();
 // var datad = $.parseJSON(data);
 // alert(data.prep);

 campos = campos+1;
 var NvoCampo= document.createElement("div");
 $('#prueba').attr('value','');
 $('#subespecifica-descripcion').attr('value','');
 $('#partidaasociada-presupuestop').attr('value','');
 $('#partidaasociada-totalp').attr('value','');
 NvoCampo.id= "divcampo_"+(campos);
 NvoCampo.innerHTML= 
 "<table class='table table-striped'  >"+
    "   <tr>" +
 "     <td>" +
 "        <input type='text' size='20' name='articupart_" + campos + 
               "' id='articupart_" + campos + "' value='"+data.pruebaId+"'>" +
 "     </td>" +
 "     <td>" +
 "        <input type='text' size='70' name='articudeno_" + campos + 
               "' id='articudeno_" + campos + "' value='"+data.deno+"'>" +
 "     </td>" +
 "     <td align='right'>" +
 "        <input type='text' size='22' name='articuprep_" + campos + 
               "' id='articuprep_" + campos + "' value='"+data.prep+"'>" +     
 "     </td>" +
 "     <td align='right'>" +
 "        <input type='text' size='22' name='articutot_" + campos + 
               "' id='articutot_" + campos + "' value='"+data.tot+"'>" +
 "     </td>" +
 "     <td align='right'>" +
 "        <input type='hidden' id='c_"+campos+"' value='hola_"+campos+"'>" +
 "     </td>" +

 "   </tr>"+
 " </table>";
 var contenedor= document.getElementById("contenedorcampos");
 contenedor.appendChild(NvoCampo);

data.part = $('#articupart_'+campos).val();
data.deno = $('#articudeno_'+campos).val();
data.prep = $('#articuprep_'+campos).val();
data.tot = $('#articutot_'+campos).val();

}

我正在尝试使用我在第一个变量中创建的隐藏div来获取变量,但是我得到了一些错误,事先谢谢。

很高兴这对您有效。所以基本上这就是解决方案:

// initialise the variable if it has not been initialised before
if (typeof(window.campos)=='undefined') {
  window.campos= 0;
}

// increase by one
window.campos+=1;
在第二个脚本中,使用以下方法访问它:

$('._save').on('click', function(event){
  ...
  // access the previously stored view counter
  data.campos= window.campos;
}     

很高兴这对你有用

尝试
window.campos=XXX
设置值,并尝试使用
警报(window.campos)在保存事件中显示值。如果两个脚本都在同一个窗口中运行,则应该可以工作。如果保存脚本在另一个页面上,您需要使用查询字符串参数传递变量。两个脚本都在同一个窗口中运行,我只是不明白“XXX”的含义,您能更好地解释一下吗?非常感谢。交换您的线路
campos=campos+1
window.campos=window.campos+1(不要忘记先初始化变量,
window.campos=0;
XXX
应仅代表随机变量值;-)哦,我现在明白了,xxx只是一个var,但是我在save event中用“alert(window.campos)”得到了“undefined”,请适当地标记-这纯粹是客户端问题,所以用
php
yii2
标记它没有什么意义。
$('._save').on('click', function(event){
  ...
  // access the previously stored view counter
  data.campos= window.campos;
}