JQuery 在使用默认 select=y 时从选择列表中获取用户选择的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4379315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery Get user selected value from select list when using default select=y
提问by Arazmus
Hi I have a Select list liek so:
嗨,我有一个选择列表,所以:
<select id=cardtype name=cardtype>
<option selected='y' value="debit-0.00-50.00">Debit1</option>
<option value="debit-0.00-50.00">Debit2</option>
<option value="debit-0.00-50.00">Debit3</option>
<option value="Credit-1.00-50.00">CreditCard</option>
</select>
i have been trying a various methods in JQuery to get the value of the user selected value: var card = $('#cardType').val(); var card = $('#cardType :selected').val(); etc etc
我一直在 JQuery 中尝试各种方法来获取用户所选值的值: var card = $('#cardType').val(); var card = $('#cardType :selected').val(); 等等等等
However they all just return the value of the default selected option:
然而,它们都只是返回默认选定选项的值:
<option selected='y' value="debit-0.00-50.00">Debit1</option>
Does anyone know of a way to get the selected option value that the user selects rather than the default selected value?
有谁知道获取用户选择的选定选项值而不是默认选定值的方法吗?
EDIT to clarify the JQuery is run when a user hits a link like so:
编辑以澄清当用户点击这样的链接时运行 JQuery:
<label for="paymentAmount">Amount (minimum payment <a class="minPay" href="">£<span id="dueFrom">50.00</span></a>) <span class="instructions"><span class="mandatory">*</span></span></label>
with the following jquery:
使用以下 jquery:
$("a.minPay").click(function(event) {
event.preventDefault();
$("#paymentAmount").val($("#dueFrom").html());
var from = parseFloat($("#paymentAmount").val());
var card = $('#cardType').val();
});
This does not run when the user select an option in the select list.. and i want the value option not the text :)
当用户在选择列表中选择一个选项时,这不会运行..我想要值选项而不是文本:)
回答by Nick Craver
Make sure you're getting it with the proper id
and at the right time, this is the correct method:
确保你有适当的得到它id
,并在合适的时间,这是正确的方法:
$('#cardtype').val();
Note that the id
is cardtype
, not cardType
, it is case sensitive. Also, you need to make sure you're running after that afterthe user has selected a value, for example:
请注意id
is cardtype
, not cardType
,它区分大小写。此外,您需要确保在用户选择一个值后运行,例如:
$('#cardtype').change(function() {
alert($(this).val());
});
You can test it out here. Also note that your attributes should alwaysbe quoted.
你可以在这里测试一下。另请注意,应始终引用您的属性。
回答by Jonathon Bolster
You need to make sure that your IDs are the same (your code and HTML have different cases);
您需要确保您的 ID 相同(您的代码和 HTML 的大小写不同);
$('#cardtype').val();
will get the selected value as shown here: http://jsfiddle.net/jonathon/8hvRa/
$('#cardtype').val();
将获得选定的值,如下所示:http: //jsfiddle.net/jonathon/8hvRa/
One other point is that all your debit card values are the same. val
gets the value of the option, not the text.
另一点是您所有的借记卡价值都相同。val
获取选项的值,而不是文本。