HTML使用变量显示文本

时间:2012-08-01 06:35:19

标签: javascript html

我在HTML中创建一个复选框,如下所示:

<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />

现在,我想要I have a bike部分,即除了复选框之外的文本来自变量。怎么办呢?

2 个答案:

答案 0 :(得分:5)

<input type="checkbox" name="vehicle" value="Bike" id="vehicle" />
<label for="vehicle" id="lblVehicle"></label><br />
<script>
var lblBike = 'I have a bike';
document.getElementById('lblVehicle').innerHTML = lblBike;
</script>

答案 1 :(得分:1)

var yourTextVariable = 'I have a cool bike';
document.getElementById('vehicle').innerHTML = yourTextVariable;