2012-12-23 64 views
1

我一直在使用一種表單,我希望提交按鈕被禁用,除非選擇了textarea和select選項。啓用/禁用表單提交按鈕不起作用

textarea驗證工作正常,問題與選擇。 問題是select從自定義drowpdown中取值,它將值放在select中,我認爲這是問題,因爲腳本不會將它作爲select的值。

該腳本的工作原理是: a)首先選擇一個選項,然後鍵入textarea。 b)你輸入textarea,選擇一個選項,然後在textarea中再次輸入。

用於該腳本的啓用/禁用按鈕是本

$(function() { 
     $("#result, #content-text-ta").bind("keyup change", 
    function() {  
     if ($("#content-text-ta").val() != "" && $("#result").val() != "") 
      $(this).closest("form").find(":submit").removeAttr("disabled").removeClass('disabled').addClass('enabled'); 
     else 
      $(this).closest("form").find(":submit").attr("disabled", "disabled").removeClass('enabled').addClass('disabled');  
     }); 
     }); 

該pases值到選擇(ID = 「catselect」)

<textarea title="Post Text" name="content-text-ta" id="content-text-ta"></textarea> 
<dl id="sample" class="dropdown"> 
        <dt><a href="#" ><span class="cat" tabindex="1">Elige categoria</span></a></dt> 
         <dd style="z-index: 1;"> 
          <ul> 
           <li><a href="#">Citas Famosas<span class="value">1</span></a></li> 
           <li><a href="#">Frases Bonitas<span class="value">2</span></a></li> 
          </ul> 
         </dd> 
        </dl> 
        <fieldset"> 
         <select name='categories' id='catselect' class='tdomf_categories' size='1'> 
          <option id="result"></option> 
         </select> 
        </fieldset> 

回答

0

使用以下代碼中的文字區域和下拉:

function validateForm() { 
    if ($("#content-text-ta").val() !== "" && $("#catselect").val().length > 0) { 
     $("form").find("input[type*='submit']").removeAttr("disabled").removeClass('disabled').addClass('enabled'); 
    } else { 
     $("form").find("input[type*='submit']").attr("disabled", "disabled").removeClass('enabled').addClass('disabled'); 
    } 
} 
$(function() { 
    $("#catselect").html(""); 
    $(".dropdown dd ul li a").click(function() { 
     var text = $(this).html(); 
     $(".dropdown dt a span").html(text); 
     $(".dropdown dd ul").hide(); 
     $("#catselect").val($("#sample").find("dt a span.value").html()); 
     validateForm(); 
    }); 
    $("#catselect").append($("<option/>", { 
     value: "", 
     text: $(this).find('span').first().contents().filter(function() { 
      return this.nodeType == 3; 
     }).text() 
    })); 
    $(".dropdown dd ul li").each(function() { 
     $("#catselect").append($("<option/>", { 
      value: $(this).find(".value").text(), 
      text: $(this).find('a').first().contents().filter(function() { 
       return this.nodeType == 3; 
      }).text() 
     })); 
     $("#catselect, #content-text-ta").bind("keyup change click", validateForm); 
    });​ 

Live Demo | Source