开发者

jQuery > How to pass the value of a select list to an onclick handler?

开发者 https://www.devze.com 2023-04-05 13:00 出处:网络
I have a button element below that has an onclick handler. Inside the onclick I\'m building a dyanamic querystring. I need to make the \"template\" variable dynamic to match the val() property of the

I have a button element below that has an onclick handler. Inside the onclick I'm building a dyanamic querystring. I need to make the "template" variable dynamic to match the val() property of the currently selected item in the "template" select menu. How can I do this?

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

<button 
type="button" 
id="myButton" 
onclick="window.open('<?php echo $the开发者_运维技巧Path ?>/test.php?template=?????', 'popup'); 
return false;">
click me
</button>

I know I can grab the selected option in jQuery with $('#template :selected').val();

How can I pass this to the onclick handler?


<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>


$("#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'); 

});
0

精彩评论

暂无评论...
验证码 换一张
取 消