2016-11-29 28 views
0
<ul class="dropdown-menu"> 
    <li><a href="#" class="small" data-value="1" tabindex="-1"> 
     <input type="checkbox" id="Speaker1" class="chkCris" value="1" />1</li> 
      <li><a href="#" class="small" data-value="2" tabindex="-1"> 
       <input type="checkbox" id="Speaker2" class="chkCris" value="2" />2</a> </li> 
      <li><a href="#" class="small" data-value="6" tabindex="-1"> 
       <input type="checkbox" id="chkOthers" class="chkCriOthers" value="Others" />&nbsp;Others, please explain</a> </li> 
      <li><div id="divOthers" style="display: none; font-size: 14px; text-align: left;"><a id="A1" href="#" class="small" data-value="7" tabindex="-1"> 
     <input class="input-xxlarge" id="textboxOthers" name="textboxOthers" type="text"        placeholder="Others, please explain" ></a> 
     </div> 
    </li> 
</ul> 

function getValueCriteriaSpeaker() 
{ 
    /* declare an checkbox array */ 
    var chkArray = []; 
    $(".chkCris:checked").each(function() { 
     chkArray.push($(this).val()); 
    }); 

    if (document.getElementById("chkOthers").checked == true) 
    { 
     var otherscriteria = document.getElementById("textboxOthers").value; 
     chkArray.push(otherscriteria); 
    } 

    // .... 

如何打印空白如果我取消選中複選框? 例如:值2取消選中(打印空格) - > 1,其他如何打印空白,如果我取消選中複選框,當我檢查拆分(「,」)

+5

有錨標記的輸入是壞的,非常糟糕 –

+0

另一個是如果你使用jQuery,那麼堅持jQuery不要將jQuery與javascript混合使用。 –

回答

1

雖然問題不是很清楚,但這裏是我實現您的問題

function getValueCriteriaSpeaker(){ 
 
    var values = $("ul.dropdown-menu :checkbox:checked").map(function(i,cb){ return $(cb).val();}); 
 
    var str = values.get().join($('#useComma').is(':checked') ? ',' : ' '); 
 
    
 
     $('#textboxOthers').val(str); 
 
    } 
 

 
$('.chkBox').on('change',getValueCriteriaSpeaker) 
 
$('#useComma').on('change',getValueCriteriaSpeaker)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<span> 
 
    <input type="checkbox" id="useComma"/> 
 
    Split By "," (Comma) 
 
</span> 
 
<ul class="dropdown-menu"> 
 
      <li><input type="checkbox" id="Speaker1" class="chkBox" value="1" />1</li> 
 
      <li><input type="checkbox" id="Speaker2" class="chkBox" value="2" />2</li> 
 
      <li><input type="checkbox" id="chkOthers" class="chkBox" value="Others">Others</li> 
 
      <li> 
 
       <div id="divOthers"> 
 
        <input class="input-xxlarge" id="textboxOthers" name="textboxOthers" type="text"        placeholder="Others, please explain"/> 
 
       </div> 
 
      </li> 
 
</ul>

相關問題