开发者

How to select option in select list using jquery

开发者 https://www.devze.com 2023-01-31 00:47 出处:网络
i have a form that has select element <select name=\"adddisplaypage[]\" id=\"adddisplaypage\" multiple=\"multiple\">

i have a form that has select element

<select name="adddisplaypage[]" id="adddisplaypage" multiple="multiple">
    <option value="all" label="all">all</option>
    <option value="index" label="index">index</option>
    <option value="tour" label="tour">tour</option>
    <option value="aboutus" label="about us">about us</option>
    <option value="contactus" label="contact us">contact us</option>
    <option value="des开发者_JAVA技巧tination" label="destination">destination</option>
    <option value="reservation" label="reservation">reservation</option>
</select>

can anyone help me to select this option (multiple select) on click i.e the option gets selected when clicked, and deselected if selected on click.


I realized I may have misunderstood your question. Something like the following should work, though I'm not sure about browser support:

$('#adddisplaypage option').click(function(e) {
  e.preventDefault();
  var self = $(this);

  if(self.attr('selected') == '') {
    self.attr('selected', 'selected');
  } else {
    self.attr('selected', '');
  }
});


In your click() handler, you could write something like:

$("#adddisplaypage").val("index");

That should select "index", for example.


You can pass an array to the .val() method. For instance:

$('#adddisplaypage').val(['index', 'tour']);
0

精彩评论

暂无评论...
验证码 换一张
取 消