使用split()从选择下拉列表填充文本字段值

时间:2013-11-19 04:12:58

标签: javascript php jquery split

当我使用split()时,textfield值为空,从我需要的下拉文本字段的值是例如15。 请告诉我哪里出错了.. 感谢,

这是代码:

<select name="cmbitems" id="cmbitems">
    <option value="price1:15">blue</option>
    <option value="price2:20">green</option>
    <option value="price3:25">red</option>
</select>

<input type="text" name="txtprice" id="txtprice" onClick="checkPrice()">

var select = document.getElementById('cmbitems');
var pecah = select.split(":");
var hasil = pecah[1];

var input = document.getElementById('txtprice');
select.onchange = function() {
    input.value = hasil.value;
}

2 个答案:

答案 0 :(得分:0)

尝试

var input = document.getElementById('txtprice');
document.getElementById('cmbitems').onchange = function() {
var select = document.getElementById('cmbitems').value;
var pecah = select.split(":");
var hasil = pecah[1];
alert(hasil);
input.value = hasil;
}

答案 1 :(得分:0)

试试这个

<select name="cmbitems" id="cmbitems">
    <option value="price1:15">blue</option>
    <option value="price2:20">green</option>
    <option value="price3:25">red</option>
</select>

<input type="text" name="txtprice" id="txtprice">

var input = document.getElementById('txtprice');
var select = document.getElementById('cmbitems');
select.onchange = function() {
        var pecah = select.options;
        var hasil = pecah[pecah.selectedIndex];
    input.value = hasil.value.split(":")[1];
 }