2017-09-12 34 views
0

如何在javaScript中传递变量单击处理程序。在JavaScript单击处理程序之间传递变量

我已将var condition_selction = ($(this).val());分配给.change(function)。我如何在第二个点击句柄$("#process-signs-scrap-scrapped-button-enabled").click(function() {中获得这个变量的值。

我如何在这两个函数之间传递变量。

$('input:checkbox').change(function() { 
     /*get the parent node of the radio and then hide only its siblings*/ 
     $(this).parent('label').siblings().toggle(); 
     if ($('.sign-condition').is(':checked')) { 
      var condition_selction = ($(this).val()); 
     } 
     if ($('.sign-reason').is(':checked')) { 
      var reason_selction = ($(this).val()); 
     } 
     //Check that both select fields have a value 
     if (condition_selction != null && reason_selction != null) { 
      $("#process-signs-scrap-scrapped-button-disabled").hide(); 
      $("#process-signs-scrap-scrapped-button-enabled").show(); 
     } else { 
      $("#process-signs-scrap-scrapped-button-disabled").show(); 
      $("#process-signs-scrap-scrapped-button-enabled").hide(); 
     } 
    }); 

    $("#process-signs-scrap-scrapped-button-enabled").click(function() { 



      var process_signs_scrap_condition = condition_selction; 


      var process_signs_scrap_reason = reason_selction 


      // Open the timer modal 
      $('#process-signs-scrap-scrapped-modal').modal('show'); 
+2

什么相同的两个二元函数的全局变量或变量范围是什么? –

回答

1

声明在全球VAR,这样你就可以在这两个函数访问它们

var condition_selction; // like this 
var reason_selction; 

$('input:checkbox').change(function() { 
    /*get the parent node of the radio and then hide only its siblings*/ 
    $(this).parent('label').siblings().toggle(); 
    if ($('.sign-condition').is(':checked')) { 
     condition_selction = ($(this).val()); // removed declaration 
    } 
    if ($('.sign-reason').is(':checked')) { 
     reason_selction = ($(this).val()); 
    } 
    //Check that both select fields have a value 
    if (condition_selction != null && reason_selction != null) { 
     $("#process-signs-scrap-scrapped-button-disabled").hide(); 
     $("#process-signs-scrap-scrapped-button-enabled").show(); 
    } else { 
     $("#process-signs-scrap-scrapped-button-disabled").show(); 
     $("#process-signs-scrap-scrapped-button-enabled").hide(); 
    } 
}); 

$("#process-signs-scrap-scrapped-button-enabled").click(function() { 



     var process_signs_scrap_condition = condition_selction; 


     var process_signs_scrap_reason = reason_selction 


     // Open the timer modal 
     $('#process-signs-scrap-scrapped-modal').modal('show'); 
0

您可以将价值传递到按钮上的数据属性,然后单击按钮访问数据的onclick属性和它的好处去。我已经修改了一些代码来获取示例版本。

$('input:checkbox').change(function() { 
 
     if ($('.sign-condition').is(':checked')) { 
 
      var condition_selction = $(this).val(); 
 
      $("#process-signs-scrap-scrapped-button-enabled").attr('data-value', condition_selction); 
 
     } 
 
    }) 
 

 
    $("#process-signs-scrap-scrapped-button-enabled").click(function() { 
 
      var condition_selction = $(this).attr('data-value'); 
 
      console.log("The value of the checkbox is " + condition_selction); 
 
      })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<label> 
 
    <input type="checkbox" class="sign-condition" value="sign condition" /> click me first 
 
</label> 
 
<hr/> 
 
<button type="button" data-value="" id="process-signs-scrap-scrapped-button-enabled">click me last</button>