ID分配在点击功能范围之外无效 - Jquery

时间:2015-06-05 12:00:47

标签: javascript jquery scope

我需要从click函数内部为变量分配一个ID,但是也要从函数范围之外进行该赋值,尽管如果我在click函数之外测试它,我会得到一个'undefined'。(我没有问下面的答案,我的错误)

// var for button
var $firstMobButton;

 $(".preLoaderTest").click(function(){

     var checkDiv = $(this);

     if(checkDiv.attr("id") == "showPreLoader"){
          $firstMobButton = $("#showPreLoader");

         alert($firstMobButton); // object

     }else if(checkDiv.attr("id") == "showPreLoaderDebt"){
         $firstMobButton = $("#showPreLoaderDebt");

         alert($firstMobButton); // object
     }
 });
   // this function doesnt assign the variable to the id OUTSIDE the If statement ...thats what i meant, Sorry i didnt explain well, i didnt mean what it is on instant load, i mean what is it after this function gets carried out)

解决了:

   $(".preLoaderTest").click(function(){
    var checkDiv = $(this);
    if(checkDiv.attr("id") == "showPreLoader"){ // check for an id for this partial view

最好的方法不是尝试分配一个ID,而是给按钮一个类,然后在按钮有ID的情况下稍后进行测试,这要归功于Quentin让我走在正确的轨道上......

如果您有兴趣,可以使用完整的解决方案:

    // grab dataSource for unselected value
    var $yourAge = yourAge.view(); // change this dataSource value
    // function call on click if onSelect empty html
    $(".preLoaderTest").click(function(){
        var checkDiv = $(this);
        if(checkDiv.attr("id") == "showPreLoader"){ // check for an id for this partial view
        if($("#displayNextSelection").html() === "") {
            $("#displayNextSelection").html('<p class="selectFeedBack">You age: ' + '<b>' + $yourAge[0].text + '</b>' + '</p>');
            $disDeskSel2.html('<p><span class="label label-primary label-ls">Based on your selection </span> You age: ' + $yourAge[0].text + '</p>')
                .hide();
            }
        }
    });

3 个答案:

答案 0 :(得分:3)

  

在点击事件之前,它会始终显示未定义

这是因为您的alert($firstMobButton);在点击方法之前执行。

点击完成后,您可以获得$firstMobButton的值。像:

$(".preLoaderTest").click(function(){

     var checkDiv = $(this);

     if(checkDiv.attr("id") == "showPreLoader"){
          $firstMobButton = $("#showPreLoader");

         alert($firstMobButton); // object

     }else if(checkDiv.attr("id") == "showPreLoaderDebt"){
         $firstMobButton = $("#showPreLoaderDebt");

         alert($firstMobButton); // object
     }
     checkValue();//calling after click is done

 });

function checkValue() {
    alert($firstMobButton); 
}

另一个解决方案是您可以在警告之前手动trigger点击事件:

$(".preLoaderTest").click(function(){

     var checkDiv = $(this);

     if(checkDiv.attr("id") == "showPreLoader"){
          $firstMobButton = $("#showPreLoader");

         alert($firstMobButton); // object

     }else if(checkDiv.attr("id") == "showPreLoaderDebt"){
         $firstMobButton = $("#showPreLoaderDebt");

         alert($firstMobButton); // object
     }

 });

$(".preLoaderTest").trigger('click')// manually triggering click event
alert($firstMobButton); 

答案 1 :(得分:1)

单击preLoaderTest时,您可以为class ViewController: UIViewController { @IBOutlet weak var btnEmergency: UIButton! override func viewWillAppear(animated: Bool) { self.btnEmergency.backgroundColor = UIColor.redColor(); } override func viewDidLoad() { super.viewDidLoad() } @IBAction func btnEmergencyTapped(sender: AnyObject) { let emergencyViewController = storyboard?.instantiateViewControllerWithIdentifier("EmergencyViewController") as! EmergencyViewController emergencyViewController.emergencyLabel?.text = "Notruf" navigationController?.pushViewController(emergencyViewController, animated: true) } } 指定一个值

您尝试立即提醒 (这将在此之前,因为您刚刚注册了事件处理程序)。

JavaScript不能违反线性时间规则!

答案 2 :(得分:0)

由于在点击按钮之前未分配变量,因此在此之前它将为undefined。运行单击后,您将能够使用该变量,并且在全局范围内定义变量时分配变量。

您的代码执行如下:

 var $firstMobButton; // variable $firstMobButton is defined with undefined value
 $(".preLoaderTest").click(function(){
     // a click handler is attached to an element
 });
 $(".test").click(function () {
     alert($firstMobButton); // click this one after the preLoaderTest
 });
 alert($firstMobButton); // Your variable is still not assigned to, so undefined