admin管理员组文章数量:1404054
I am trying to simulate a javascript to choose from the dropdown list below. I am trying this code here but its not working.
$('select[name="srctype"]').val(2).trigger('change');
Am i doing something wrong here? Is my javascript messed up for this layout of code below?
I am trying to choose value="single"
<select name="srctype" class="formselect" onchange="typesel_change()">
<option value="any" selected="selected">any</option>
<option value="single">Single host or alias</option>
<option value="network">Network</option>
<option value="pptp">PPTP clients</option>
<option value="pppoe">PPPoE clients</option>
<option value="l2tp">L2TP clients</option>
<option value="wan">WAN subnet</option>
<option value="wanip">WAN address</option>
<option value="lan">LAN subnet</option>
<option value="lanip">LAN address</option>
</select>
I am trying to simulate a javascript to choose from the dropdown list below. I am trying this code here but its not working.
$('select[name="srctype"]').val(2).trigger('change');
Am i doing something wrong here? Is my javascript messed up for this layout of code below?
I am trying to choose value="single"
<select name="srctype" class="formselect" onchange="typesel_change()">
<option value="any" selected="selected">any</option>
<option value="single">Single host or alias</option>
<option value="network">Network</option>
<option value="pptp">PPTP clients</option>
<option value="pppoe">PPPoE clients</option>
<option value="l2tp">L2TP clients</option>
<option value="wan">WAN subnet</option>
<option value="wanip">WAN address</option>
<option value="lan">LAN subnet</option>
<option value="lanip">LAN address</option>
</select>
Share
Improve this question
edited Sep 6, 2013 at 19:32
Leniel Maccaferri
102k46 gold badges378 silver badges493 bronze badges
asked Sep 6, 2013 at 19:12
soniccoolsoniccool
6,06823 gold badges64 silver badges101 bronze badges
3 Answers
Reset to default 5Do this:
$('.formselect').val('single').trigger('change');
In your code you're trying to set a val 2
that doesn't exist. The value you're interested is actually single
as you mention in your question.
Here's a demo: http://jsbin./eXONuhu/1/
Try:
$('select[name="srctype"]').val('single')
or
$('select[name="srctype"] option:eq(1)').prop('selected',1)
As you noted in your ment, this worked for you:
(function ($) {
$('select[name="srctype"]').val('single');
}).call(this, jQuery);
var myVal = $('select[name="srctype"] option:eq(1)').attr('value');
$('select[name="srctype"]').val(myVal).trigger('change');
Or an optimized version
var mySelect = $('select[name="srctype"]');
var myVal = $('option:eq(1)', mySelect).attr('value');
mySelect.val(myVal).trigger('change');
Fiddle - http://jsfiddle/P6Ayz/1/
本文标签: htmlSimulate javascript to choose from dropdownStack Overflow
版权声明:本文标题:html - Simulate javascript to choose from dropdown - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744212464a2595484.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论