2013-03-21 68 views
0

我正在使用此代码来检查是否在下拉菜单中选择了一个值。禁用发送按钮

$(document).ready(function() { 
$("send").click(function() { 

if (document.getElementById('dropdown1').selectedIndex == 0) 
$("#msg_dropdown1").html("Required"); 

}); 
$('#dropdown1').change(function(){$('#msg_dropdown1').hide()}) 
}); 

存在一种方法来禁用SUBMIT按钮,直到选中一个值为止?

<input type="submit" id="send" value="SEND" class="greyishBtn" /> 
+4

是不是'$( “发送”)'错了吗? – hjpotter92 2013-03-21 04:22:09

+0

是,提交:-) – user2123738 2013-03-21 04:23:19

+0

请注意,如果未选择任何选项,则所选索引将为-1,并且测试将失败。你应该有'.selectedIndex <1' – RobG 2013-03-21 05:23:18

回答

0

变化$("send").click(function() {$(#"send").click(function() {

0

首先使用ID选择为send

Repalce$("send").click(function() {

随着$("#send").click(function() {

$('#dropdown1').change(function(){ 
     if($('#dropdown1').val() != 0){ 
      $("#send").attr("enabled","enabled"); 
     } else{ 
      $("#send").attr("disabled","disabled"); 
     } 
    }); 
+0

我已经添加了#但继续不工作:( – user2123738 2013-03-21 04:30:40

0
$(document).ready(function() { 
    $("#send").click(function() { 
     if (document.getElementById('dropdown1').selectedIndex == 0) 
      $("#msg_dropdown1").html("Required"); 
    }); 
    $('#dropdown1').change(function(){ 
     if($(this).val() != 0){ 
      $("#send").removeAttr("disabled"); 
     } 
     else{ 
      $("#send").attr("disabled","disabled"); 
     } 
    }); 
}); 

<input type="submit" id="send" value="SEND" class="greyishBtn" disabled="disabled" /> 
0

试试这个

<select id="dropdown1" name="dropdown1" onchange="unblock(this)"> 

    function unblock(arg){ 
     if(arg.options[arg.selectedIndex].value !=0){ 
     $('#send').css('display','block'); 
     } 
    }