2011-10-04 59 views
1

請幫忙。變量如何傳遞?我有功能選擇選項jQuery變量如何通過?

例 的jQuery: -

var one = '1'; 
var two = '2'; 

$(document).ready(function(){ 
    $(#).change(function(){ 
     if(one == '1'){ 
      var one = 'hello'; 
     } 
    }); 

    $(#).change(function(){ 
     if(two == '2'){ 
      var two = 'world!'; 
     } 
    }); 

    if (one =='hello' && two =='world!'){ 
     $(#welcome).hmtl("hello world!") 
    } 
}); 
+0

你可能想澄清你的問題,我不是很確定你是問什麼。 –

回答

0

非常感謝您的回答

我發現

我的示例代碼: -

$(document).ready(function(){ 

    var one, two; 

    $("#item_select1, #item_select2").change(function(){ 

     one = $("#item_select1").val(); 
     two = $("#item_select2").val(); 

     if (one =='1' && two == '2'){ 
      $("#welcome").html("hello world!"); 
     } 

     if (one =='1' && two == '1'){ 
      $("#welcome").html(""); 
     } 

     if (one =='0' && two == '0'){ 
      $("#welcome").html("Goodbye!"); 
     } 

    }); 

}); 
1

你應該聲明變量作爲全局,以能夠從任何地方訪問它們在你的代碼,你不應該使用var關鍵字再次聲明他們:

$(document).ready(function(){ 

var one, two; 
$("selector").change(function(){ 


if(one == '1'){ 

    one = 'hello'; 

} 


    }); 


$("selector").change(function(){ 


if(two == '2'){ 

    two = 'world!'; 

} 


    }); 

if (one =='hello' && two =='world!'){ 

$(#welcome).hmtl("hello world!") 

} 
0

使用此功能:

$(document).ready(function(){ 
    radio = $('.radioid option:selected').val(); 

    if(radio == 1) 
    alert(1); 

    ... 
}); 
+0

謝謝,可以請更多的例子/ – user692669