如何在html中打印jquery值

how to print jquery value in html

本文关键字:打印 jquery html      更新时间:2023-09-26

这是我的代码在jQuery,它的工作很好,当它的警报弹出,但我想打印这个在html体我下面尝试,但不工作

// Do What You Want With Result .......... :)
$("#printermodel").change(function() {
    //'you select     
    Model = ' + $('#manufacturer').val() + 'type=' + $('#printertype').val() + 'And Model=' + $('#printermodel').val()
    alert('you select Model = ' + $('#manufacturer option:selected').text() + ' ,type= ' + $('#printertype option:selected').text() + ' And Model = ' + $('#printermodel option:selected').text());
});
});
<div id="manufacturer"></div>

您应该使用textdiv中插入文本:

$('#manufacturer').text($('#manufacturer option:selected').text());

文档:http://api.jquery.com/text/

如果你想在div里面添加HTML,你可以用html代替text

$('#manufacturer').html(('you select Model = '+
$('#manufacturer option:selected').text()+' ,type= '+
$('#printertype option:selected').text()+' And Model = '+
$('#printermodel option:selected').text());

如果您想采用更复杂的方法,我建议您使用模板。其中一个选择就是使用官方的jquery-tmpl插件。

你需要包括jquery和http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js插件到你的网站,然后你可以做,例如:

Html:

<div id="target"></div>
Javascript:

$.tmpl( "<li>${Name}</li>", { "Name" : "John Doe" }).appendTo( "#target" );

您可以根据需要进行调整,例如从某些文件

中读取模板(tmpl的第一个参数)