0

这似乎是一件相当简单的事情,但这个问题已经给我带来了很长一段时间的麻烦。I have a submit button that is 'disabled' when a select list value is 'select'. 我想要的是 html('select an area')...但是按钮被禁用。这是代码:

<script>
//setting the variable for first-shown select value
var selVar = $('#areaSel').val();
//disabling submit if first-shown value is 'select'
if(selVar == 'select'){
    $('#areaSubmit').prop('disabled',true);
}
//setting the variable if the selection changed & disabling submit if = 'select'
$('#areaSel').change(function(){
    var selVar = $('#areaSel').val();
    if(selVar == 'select'){
       $('#areaSubmit').prop('disabled',true);
    //removing 'disabled' submit if selection !== select
    }else{
        $('#areaSubmit').prop('disabled',false);
    }
});
    //giving the message to 'select an area
$('#areaSubmit').click(function(){
    var selVar = $('#areaSel').val();
    if(selVar == 'select'){
    $('#areaE').html('select an area');
    }
});
</script>

我只是想让消息“选择一个区域”出现,而不是在单击时提交。问题是选择“选择”时禁用按钮。在此先感谢您的帮助!

4

2 回答 2

1

当提交按钮被禁用时,点击事件不会触发;所以你的标签的 html 不会改变。将相对于更改“areaE”内容的代码移动到其他位置,可能是您选择的更改事件

http://jsfiddle.net/QFWYE/1/

var selVar = $('#areaSel').val();
//disabling submit if first-shown value is 'select'
if(selVar == 'select'){
    $('#areaSubmit').prop('disabled',true);
}
//setting the variable if the selection changed & disabling submit if = 'select'
$('#areaSel').change(function(){
    var selVar = $('#areaSel').val();
    if(selVar == 'select'){
       $('#areaSubmit').prop('disabled',true);
         $('#areaE').html('select an area'); 
    //removing 'disabled' submit if selection !== select
    }else{
        $('#areaSubmit').prop('disabled',false);
         $('#areaE').html(''); 
    }
});
    //giving the message to 'select an area
$('#areaSubmit').click(function(){
    var selVar = $('#areaSel').val();
});
于 2012-11-06T13:55:42.783 回答
0

给你,我相信这就是你的追求。

//giving the message to 'select an area
$('#areaSubmit').click(function(event){
    event.preventDefault();
    selVar = $('#areaSel').val();
    if(selVar == 'select'){
    $('#areaE').html('select an area');
    }else{$('#form').submit();
    }
});
于 2012-11-06T13:33:14.317 回答