如何使用$?在文本区域中加载文件内容?

时间:2013-09-02 14:52:08

标签: javascript jquery ajax

我想在文本区域中显示样式表内容是用户在选择框中选择样式。样式表在服务器中,所以我尝试加载,并且两者都不起作用。

<script type="text/javascript">
    jQuery(document).ready(function(){
    jQuery('#sel').change(function() { 
     jQuery.get('style1.css',function(data) {
     jQuery('#cs').html(data);
     alert('Load was performed.');
     });

   // jQuery('#cs').load('style1.css'); 

    }); 
});
    </script> 

HTML

<select id="sel">
 <option value="style1">style1</option>
 <option value="style2">style2</option>
</select>

我正在加载已执行警报但未执行文本区域中的内容。

此处#cs是我的文字区域的ID。

2 个答案:

答案 0 :(得分:1)

使用val()而不是html()设置textarea的值。

答案 1 :(得分:1)

你需要使用val。

jQuery(document).ready(function(){
    jQuery('#sel').change(function() { 
     jQuery.get('style1.css',function(data) {
     jQuery('#cs').val(data);
     alert('Load was performed.');
     });

   // jQuery('#cs').load('style1.css'); 

    });