使用href中的onclick更改选择列表的值

时间:2011-09-24 10:28:36

标签: javascript select onclick

我想使用。的onClick函数更改选择框值。

 <form>
      <select id="action">
         <option value="a">Add</option>
         <option value="b">Delete</option>
      </select>
 </form>

<a href="#" onClick="">Add</a> 
<a href="#" onClick="">Delete</a> 

4 个答案:

答案 0 :(得分:7)

 <form>
      <select id="action">
         <option value="a">Add</option>
         <option value="b">Delete</option>
      </select>
 </form>

<a href="#" onClick="document.getElementById('action').value='a'">Add</a> 
<a href="#" onClick="document.getElementById('action').value='b'">Delete</a> 

OR U也可以调用此Javascript函数来执行此操作=

function changeval()
{

if(document.getElementById('action').value =='b')
document.getElementById('action').value='a'
else
document.getElementById('action').value='b'

}

<a href="#" onClick="changeval()">Add</a> 
<a href="#" onClick="changeval()">Delete</a> 

答案 1 :(得分:0)

我认为这是您正在寻找的地方:

<script type="text/javascript">
function changeValue(selectBox, value) {
  selectedItem = selectBox[selectBox.selectedIndex];
  selectedItem.value = value;
}
</script>

在onclick中你放置“

    changeValue(document.getElementById("action"), "your value");

来自:http://www.webdeveloper.com/forum/showthread.php?t=160118

答案 2 :(得分:0)

<form>
  <select id="action">
     <option value="a">Add</option>
     <option value="b">Delete</option>
  </select>
</form>

<a href="#" onClick="action.value='a'">Add</a> 
<a href="#" onClick="action.value='b'">Delete</a> 

答案 3 :(得分:0)

我使用了jquery .click并给了select元素一个id 这是html代码

<select id="carsList">
  <option value="volvo" id="#volvoOption">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi" id="#audiOption">Audi</option>
</select>

<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRgoru_AtWqrY6DWtmVlOvtYoxpdGHlheWUKgn0jpy0R7Z2VjjjDBh78cx1" id="volvoLogo">
<img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQBKzqqECaAC5n-cij4sToayeEAjQqAzYcIrRDj1MeP5vo5OlUCvq_CPWHx" id="audiLogo">

这是我使用的jquery代码

$( document ).ready(function() {

    $("#volvoLogo").click(function(){
          $("#carsList option[value='volvo']").attr('selected', 'selected');
    });

    $("#audiLogo").click(function(){
         $("#carsList option[value='audi']").attr('selected', 'selected');
    });
});