开发者

.val() of jquery not working for select element on IE

开发者 https://www.devze.com 2023-01-07 07:03 出处:网络
I tried all these for selected value of select element inventory_rule = $(\"#inventory_rule :selected\").attr(\'value\');

I tried all these for selected value of select element

inventory_rule = $("#inventory_rule :selected").attr('value');
inventory_rule = $("#inven开发者_如何学Ctory_rule option:selected").val();
inventory_rule = $("#inventory_rule").attr('value');
inventory_rule = $("#inventory_rule").val();

these all worked well in mozilla but not in IE

is any alternative


The 4th of your attempts is the most straightforward way to get the selected value of a select element and it should work.

var inventory_rule = $("#inventory_rule").val();

I wrote a quick and dirty example for you at jsfiddle.net, showing that it works. This means that your selector is probably wrong. Check and make sure the select element has id="inventory_rule" and make sure that id attribute is also unique on the page. Don't forget the var keyword if it's the first time you're declaring the variable.

EDIT: highlighted the part about making sure the id attribute is unique, a non-unique id will definitely cause problems in IE.


Just encountered this earlier.

when getting the value of a

<select>

tag IE 10 inserts spaces before and after the value. so just trim the value and it will solve your worries.

var inventory_rule = $.trim($("#inventory_rule").val());


You could try the jQuery Form Plugin.

0

精彩评论

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