在文本值中插入Javascript

时间:2015-06-25 19:23:49

标签: javascript html

如何在我的"价格"中插入此javascript?文字值?这是我的代码:



 <script>
function myFunction() {
    var x = document.getElementById('car-select')[document.getElementById('car-select').selectedIndex].innerHTML;
    document.getElementById("demo").innerHTML =  x;
}
</script>
&#13;
<div class="input-group pick-up">

<span class="input-group-addon"><span class="glyphicon glyphicon-map-marker"></span>Price </span>

<input name="price" type="text" class="form-control autocomplete-location" id="price" value="">

</div>

<p id="demo"></p>
                           
                           
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

正如MiiinimalLogic所说,只是尝试添加

document.getElementsByName("price")[0].value =  x;

如果您想在价格输入中显示x

如果您想在价格字段更改时调用此函数,请改为使用

document.getElementsByName("price")[0].onchange=function(){

    var x = document.getElementById('car-select')[document.getElementById('car-select').selectedIndex].innerHTML;
    document.getElementById("demo").innerHTML =  x;
};