在后期wordpress中的文本字段(javascript)中显示下拉列表的选定值

时间:2015-09-16 11:14:38

标签: javascript php jquery wordpress

我正在尝试从wordpress帖子的文本字段中的下拉列表中显示所选值。但是代码没有取值。

<select name="cmbitems" id="cmbitems"><style type="text/javascript">
var select = document.getElementById('cmbitems');
var input = document.getElementById('txtprice');
select.onchange = function() {
input.value = select.value;
}
</script>
<option value="" selected="selected">Select Country</option>
<option value="£0.0">Ireland</option>
<option value="£2.50">United States</option>
<option value="£2.50">United Kingdom</option>
<option value="£2.50">Afghanistan</option>
<option value="£2.50">Albania</option>
<option value="£2.50">Algeria</option>
<option value="£2.50">American Samoa</option>
<option value="£2.50">Andorra</option>

    邮费:

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

3 个答案:

答案 0 :(得分:1)

您的html语法无效,script标记位于select标记之外,在select标记内应该只有option个标记。

&#13;
&#13;
<script>
  var select = document.getElementById('cmbitems');
  var input = document.getElementById('txtprice');
  select.onchange = function() {
    input.value = select.value;
  }
</script>
<select name="cmbitems" id="cmbitems">
  <option value="" selected="selected">Select Country</option>
  <option value="£0.0">Ireland</option>
  <option value="£2.50">United States</option>
  <option value="£2.50">United Kingdom</option>
  <option value="£2.50">Afghanistan</option>
  <option value="£2.50">Albania</option>
  <option value="£2.50">Algeria</option>
  <option value="£2.50">American Samoa</option>
  <option value="£2.50">Andorra</option>
</select>
<input id="txtprice" />
&#13;
&#13;
&#13;

答案 1 :(得分:0)

每当您的下拉值更改时,它将更改输入字段值中的值。您可以浏览以下代码:

<script>
   function getValue(value) {
    alert(value);
    $('#txtprice').val(value);
  }
</script>
<select name="cmbitems" id="cmbitems" onchange="getValue(this.value)">
  <option value="" selected="selected">Select Country</option>
  <option value="£0.0">Ireland</option>
  <option value="£2.50">United States</option>
  <option value="£2.50">United Kingdom</option>
  <option value="£2.50">Afghanistan</option>
  <option value="£2.50">Albania</option>
  <option value="£2.50">Algeria</option>
  <option value="£2.50">American Samoa</option>
  <option value="£2.50">Andorra</option>
</select>
<input id="txtprice" />

答案 2 :(得分:0)

第一行中的脚本启动有误:

<select name="cmbitems" id="cmbitems"> <**style** type="text/javascript">

尝试使用正确的脚本标记而不是

 <**style** type="text/javascript">
 <**script** type="text/javascript">