在下拉列表上使用jQuery val() [英] Using Jquery val() on a dropdown list

查看:88
本文介绍了在下拉列表上使用jQuery val()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除其他外,我试图提醒金额"下拉框中的所选值,该框中是1到10的数字列表.将生成该列表,然后将其插入到div中.

Among other things I am trying to alert the selected value in the 'Amount' drop down box which is a list of numbers from 1 to 10. The list is generated and then inserted in to a div.

// generate the amount dropdown list
var dropdown = "<select name=\"amount\">";
for(var i=1; i <= MAX_PRINT; i++) {
    dropdown = dropdown + "<option>" + i + "</option>"
}
dropdown = dropdown + "</select>";

jQuery("#" + divId).html("<div>Amount: " + dropdown + "</div></div>");


var amount = 1;

jQuery(document).ready(function() {
    amount = jQuery("#amount").val();
    alert(amount);
});

我的问题是:当我提醒金额时,为什么会产生未定义"?我希望它返回所选的数字

My question is: Why does it produce 'undefined' when I alert the amount? I am expecting it to return the selected number

推荐答案

请确保为您的选择提供一个ID,现在它只有一个名称.结果,您的jQuery('#amount')选择器未返回任何内容,这就是显示警报的原因 undefined.

Make sure you give your select an id—right now it just has a name. As a result, your jQuery('#amount') selector is not returning anything, which is why the alert is showing undefined.

var dropdown = "<select id='amount' name='amount'>";

这篇关于在下拉列表上使用jQuery val()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆