簡體   English   中英

我如何在2個函數中使用相同的私有變量

[英]How can i use same private variable in 2 functions

所以即時通訊有2個功能。 問題出在styep_id變量中。 我知道我可以在第二個函數中聲明它,但是他不會從第一個函數中取出數據。 所以問題是how i can use the same variable without lost data on him

PS它不應該是公共變量,因為它不能工作。 它不會保存數據。

    function delete_estimate_position_type() {
        var estpt_tr_jqobj, estpt_action_links_td_jqobj, styep_id, authenticity_token, request_url, stya_id;

        styep_id = $(this).attr("styep_id");

        // Ja ir tikko kā pievienots, tad tikai izmetīsim ārā no DOM
        if (!styep_id == "") {
            estpt_action_links_td_jqobj = $(this).parent();
            estpt_tr_jqobj = estpt_action_links_td_jqobj.parent();
            stya_id = $("td.service-type-est-position-estimate-position-type-name>input.stya-id-for-styep", estpt_tr_jqobj).val();
            estpt_tr_jqobj.remove();
            show_stya_delete_link_if_possible(stya_id);
            remove_estpgt_if_has_no_estpt($(this).attr("estpgt_id"));
        }
}

function save_configuration(){
  var estpt_for_estpgt = "";
  // Pārbaudam vai visām tāmes pozīciju grupām ir norādītas tāmes pozīcijas
  $('.estpt-for-estpgt').each(function(){
    if ($(this).find('tr.action_record_tr').size() == 0){
      estpt_for_estpgt = this;
      return false;
    }
  })

  if (estpt_for_estpgt == "") {
    var form = $(this).closest('form');
    form.submit();
            // Dzēsīsim ārā no datu bāzes
            authenticity_token = $("#authenticity_token").val();
            request_url = "/service_type_est_positions/" + styep_id + "/destroy_from_service_type_config";

            $.post(request_url, { authenticity_token: authenticity_token}, process_service_type_est_position_delete, "json");
  } else {
    $.alerts.okButton = 'Labi';
    jError("Vismaz vienai Tāmes pozīciju grupai nav norādīta neviena Tāmes pozīcija!", "Kļūda");
  }

  return false;
}

function remove_estpgt_if_has_no_estpt(estpgt_id) {
    // Paskatīsimies vai eksistē kāda tāmes pozīcija
    if ($("#estpt_for_" + estpgt_id + ">tr:first").size() == 0) {
        $("#estpgt_" + estpgt_id).remove();
        $("#estpt_tr_for_" + estpgt_id).remove();
    }
}

在第一個函數中調用第二個函數,並將變量作為參數傳遞:

function delete_estimate_position_type() { 
   save_configuration(styep_id)
}

function save_configuration(id){
   request_url = "/service_type_est_positions/" + id + "/destroy_from_service_type_config";
}

你可以這樣使用

 function delete_estimate_position_type() {
        var estpt_tr_jqobj, estpt_action_links_td_jqobj, styep_id, authenticity_token, request_url, stya_id;

        styep_id = $(this).attr("styep_id");

       // your other code goes here.....
       // Your variable pass as argument
            remove_estpgt_if_has_no_estpt($(this).attr("estpgt_id"),styep_id.val() );
        }
}

//從參數獲取。

function remove_estpgt_if_has_no_estpt(estpgt_id,styep_id ) {
    // Paskatīsimies vai eksistē kāda tāmes pozīcija
    if ($("#estpt_for_" + estpgt_id + ">tr:first").size() == 0) {
        $("#estpgt_" + estpgt_id).remove();
        $("#estpt_tr_for_" + estpgt_id).remove();
    }
}

另一個選項是在隱藏字段中設置它。

// html

<input type = "hidden" id="styep_id_newval" value="">

// html的結尾

function delete_estimate_position_type() {
        var estpt_tr_jqobj, estpt_action_links_td_jqobj, styep_id, authenticity_token, request_url, stya_id;

        styep_id = $(this).attr("styep_id");

       // your other code goes here.....
       $("#styep_id_newval").(styep_id.val());

       // Your variable pass as argument
            remove_estpgt_if_has_no_estpt($(this).attr("estpgt_id"),styep_id.val() );
        }
}

現在,您可以在任何地方輕松訪問代碼。

$("styep_id_newval").val(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM