Javascript我无法在函数

时间:2016-04-01 12:23:45

标签: javascript function public

我用ajax调用了我的数据然后在结果中创建了变量但是我无法在其他函数中找到我的变量。如何获取变量或创建公共函数? $(“#btBilgileriAktar”)是我的新函数。我想在这个函数中获取变量。

感谢您的帮助。

$.ajax({

      type: 'POST',
      data: {
        data: gripin
      },
      datatype: 'html',
      url: '<?php echo site_url('
      servis / bring_Cari '); ?>',
      success: function(result) {

        var obj = $.parseJSON(result);

        // console.log(obj);

        $.each(obj, function(key, value) {


          // console.log(value);
          $("table#cariler tbody").append("<tr class=\"cariKayit\"><td class=\"tdCariAdi\">" + obj[key].CARI_ISIM + "</td><td class=\"tdVergiNo\">" + ((obj[key].VERGI_NUMARASI == null) ? "" : obj[key].VERGI_NUMARASI) + "</td><td class=\"tdVergiDairesi\">" + ((obj[key].VERGI_DAIRESI == null) ? "" : obj[key].VERGI_DAIRESI) + "</td><td class=\"tdCariAdres\">" + ((obj[key].CARI_ADRES == null) ? "" : obj[key].CARI_ADRES) + "</td><td class=\"tdCariTel\">" + ((obj[key].CARI_TEL == null) ? "" : obj[key].CARI_TEL) + "</td><td class=\"tdCariIlce\">" + ((obj[key].CARI_ILCE == null) ? "" : obj[key].CARI_ILCE) + "</td><td class=\"tdCariIl\">" + ((obj[key].CARI_IL == null) ? "" : obj[key].CARI_IL) + "</td><td><input type=\"checkbox\" id=\"checks\"/></td></tr>");


        });
        $('#checks').on("click", function() { //Çalışıyor
          var nAdi = $(this).parent().parent().find(".tdCariAdi").text();
          var nVno = $(this).parent().parent().find(".tdVergiNo").text();
          var nVdairesi = $(this).parent().parent().find(".tdVergiDairesi").text();
          var nAdres = $(this).parent().parent().find(".tdCariAdres").text();
          var nTel = $(this).parent().parent().find(".tdCariTel").text();
          var nIlce = $(this).parent().parent().find(".tdCariIlce").text();
          var nIl = $(this).parent().parent().find(".tdCariIl").text();

       
        });


        $('#btBilgileriAktar').on('click', function() { //Buda seçimi çekiyor ama burada sıkıntı var verileri almıyor.
          var tcLength = nVno.toString();
          if (tcLength.length == 11) {

            $("#spTc").val(tcLength);

          } else if (tcLength.length == 10) {

            $("#spVno").val(tcLength);

          } else {

            $("#spTc").val("");
            $("#spVno").val("");
          }
          $("#spCariAdi").val(nAdi);
          $("#spVergiDairesi").val(nVdairesi);
          $("#spAdres").val(nAdres);
          $("#spTel").val(nTel);
          $("#spIlce").val(nIlce);
          $("#spIl").val(nIl);
        });


      }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div class="tab-pane fade active in" id="musteri">
  <div class="form-group">
    <label class="control-label" for="focusedInput">Müşteri Adı</label>
    <input class="form-control" id="spCariAdi" type="text" value="">
  </div>
  <div class="form-group">
    <label class="control-label" for="focusedInput">Adres</label>
    <input class="form-control" id="spAdres" type="text" value="">
  </div>
  <div class="form-group">
    <label class="control-label" for="focusedInput">İlçe</label>
    <input class="form-control" id="spIlce" type="text" value="">
  </div>
  <div class="form-group">
    <label class="control-label" for="focusedInput">İl</label>
    <input class="form-control" id="spIl" type="text" value="">
  </div>
  <div class="form-group">
    <label class="control-label" for="focusedInput">Tel</label>
    <input class="form-control" id="spTel" type="text" value="">
  </div>
  <div class="form-group">
    <label class="control-label" for="focusedInput">Vergi Dairesi</label>
    <input class="form-control" id="spVergiDairesi" type="text" value="">
  </div>
  <div class="form-group">
    <label class="control-label" for="focusedInput">TC Kimlik No</label>
    <input class="form-control" id="spTc" type="text" value="">
  </div>
  <div class="form-group">
    <label class="control-label" for="focusedInput">Vergi No</label>
    <input class="form-control" id="spVno" type="text" value="">
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

因为您在另一个函数中声明变量。您需要全局变量来从不同的函数访问它们。

 var nAdi = ''; //global var declaration

 $('#checks').on("click", function() { //Çalışıyor
      nAdi = $(this).parent().parent().find(".tdCariAdi").text(); //assigning value
 });

 $('#btBilgileriAktar').on('click', function() {
     $("#spCariAdi").val(nAdi); // retrieving value
 });