Display the selected value of a Drop down list in

2019-09-20 21:07发布

I am trying to display the selected value from the drop down list in a text field in a wordpress post. But the code its not fetching the values.

<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>

Postage Fee:

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

3条回答
劳资没心,怎么记你
2楼-- · 2019-09-20 21:43

You have a mistake on script start in first line:

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

TRy to use the correct script tag instead of

 <**style** type="text/javascript">
 <**script** type="text/javascript">
查看更多
劳资没心,怎么记你
3楼-- · 2019-09-20 21:52

Whenever your drop down value changes it will change the value in input field value. You can go through the following code :

<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" />
查看更多
迷人小祖宗
4楼-- · 2019-09-20 21:56

Your html syntax is not valid put script tag outside select tag, inside select tag there should be only option tags.

<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" />

查看更多
登录 后发表回答