Change value of a select list using onclick in href

半城伤御伤魂 提交于 2019-12-08 04:00:41

问题


I want to change the select box value using the onClick function of .

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

回答1:


 <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 can also call this Java Script function for doing this =

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> 



回答2:


I think this is where you are looking for:

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

In the onclick you place"

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

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




回答3:


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



回答4:


I used a jquery .click and gave an id to the select element This is the html code

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

And this is the jquery code I used

$( document ).ready(function() {

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

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


来源:https://stackoverflow.com/questions/7538435/change-value-of-a-select-list-using-onclick-in-href

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!