2011-09-18 49 views
0

我有一個按鈕元素下面有一個onclick處理程序。在onclick中,我正在構建一個dyanamic查詢字符串。我需要使「模板」變量動態地匹配「模板」選擇菜單中當前所選項目的val()屬性。我怎樣才能做到這一點?jQuery>如何將選擇列表的值傳遞給onclick處理程序?

<select id="template">..select options</select> 

<button 
type="button" 
id="myButton" 
onclick="window.open('<?php echo $thePath ?>/test.php?template=?????', 'popup'); 
return false;"> 
click me 
</button> 

我知道我可以搶在jQuery的與$('#template :selected').val();

所選擇的選項我怎樣才能把它傳遞給onclick處理程序?

回答

1
<select id="template">..select options</select> 
<button type="button" id="myButton">click me</button> 
<script type="text/javascript"> 
    $('#myButton').click(function(){ 
     window.open('<?php echo $thePath ?>/test.php?template='+$('#template').val(), 'popup'); 
     return false; 
    }); 
</script> 
0
$("#myButton").click(function(){ 
var v = $('#template').prop('selected'); //prop available in version 1.6 and higher 
// or you can just use $('#template').val(); 
onclick="window.open('<?php echo $thePath ?>/test.php?template='+v, 'popup'); 

}); 
相關問題