无法将jQuery中定义的变量用于html

时间:2012-04-15 04:34:58

标签: javascript jquery

每次选择下拉颜色时,我都希望使用document.write()打印变量'value'。

<script type="text/javascript">
    var value = 'xyz';

             $(document).ready(function(){ 
                  $('#button1').click(function(){ 
                    value = $('#combo').val();
                  alert (value);

                    });
        }); 

    </script>
<select id  = "combo">
    <option>White</option>
    <option>Yellow</option>
    <option>Fuchisa</option>
    <option>Red</option>
    <option>Silver</option>
    <option>Gray</option>
    <option>Olive</option>
</select>
<input id="button1" type="button" value="Click!" />
<p><script type="text/javascript"> document.write(value) </script></p>

1 个答案:

答案 0 :(得分:3)

http://jsfiddle.net/2ahKH/

$('#button1').click(function() {
    value = $('#combo').val();
    $('p').html(value);
});