从表单提交的selectmenu中获取选定的值

时间:2013-11-14 10:14:19

标签: jquery jquery-ui

我需要在表单提交中获取selectmenu的选定值。

  $('#submitbutton').click(function(){
var returnValue = true;
$('#form1 .required').filter(':visible').each(function () {
inputVal = $(".dd-default option:selected").val();
alert(inputVal); -- displaying null
var input = $(this);
alert(input.val()); -- displaying null
if (!input.val()) {
   returnValue = false;
}

});
});

这里$(this).val()显示为null。但是当我在selectmenu的chnage上写相同的函数时,它会显示所选的值。

$('#form1 .required').change(function () {
var input = $(this);
input.next('ul.innererrormessages').remove();
input.removeClass('required');
alert(input.val());-- displays the value
if (!input.val()) {
    alert("error");
}
});

任何想法,为什么它没有在提交表格时显示所选值? 感谢

2 个答案:

答案 0 :(得分:0)

您必须在用户切换值后使用更改。此代码应在获取值之前运行。

$('.selectList').change(function() {
  alert($(this).val());
});
  

将.selectList替换为选择列表框的类名。

答案 1 :(得分:0)

此解决方案解决了上述问题。

$('select').each(function() {
                    var objName = $(this).attr('name');
                    if ($("select[name=" + objName + "]").css('display').indexOf("none") != -1) {
                        alert($("select[name=" + objName + "] option:selected").val());
                    }

                });
相关问题