jQuery - 启用禁用(切换)选择框onClick按钮

时间:2015-06-03 12:57:53

标签: jquery jquery-selectbox

如何在启用和禁用onClick按钮之间切换选择框...

方案: 如果我点击#locked div,则此div应与另一个类“icon-unclocked”切换,并且应启用selectbox。如果我再次点击“#locked”,则应停用选择框,并且必须删除“icon-unclocked”。

第一部分(toggleClass)效果很好..但在启用/禁用选择框时遇到问题...

  

FIDDLE

请检查以下代码......

  

HTML:

<div class="icon-locked" id="locked">&nbsp;</div>
<select class="form-control" id="signontype" disabled>
  <option>Option 01</option>
  <option>Option 02</option>
</select>
  

jQuery的:

$(document).ready(function(){
    $( "#locked" ).click(function() {
      $( this ).toggleClass( "icon-unlocked" );
      $('select#signontype').prop('disabled', false);
    });
});

4 个答案:

答案 0 :(得分:4)

试试这个:

using (SpreadsheetDocument myDoc = SpreadsheetDocument.
       Create(Server.MapPath("/data.xls"), SpreadsheetDocumentType.Workbook))
{
    WorkbookPart workbookpart = myDoc.AddWorkbookPart();
    workbookpart.Workbook = new Workbook();

    // Add a WorksheetPart to the WorkbookPart.
    WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
    //add column names to the first row  
    Row header = new Row();
    header.RowIndex = (UInt32)1;
    SheetData sheetData = new SheetData();

    Cell headerCell = createTextCell(1, 1, text);
    header.AppendChild(headerCell);

    sheetData.AppendChild(header);
    // Add a WorkbookPart to the document.
    worksheetPart.Worksheet = new Worksheet(sheetData);

    //this is the part that was missing from your code
    Sheets sheets = myDoc.WorkbookPart.Workbook.AppendChild(new Sheets());
    sheets.AppendChild(new Sheet()
    {
        Id = myDoc.WorkbookPart.GetIdOfPart(myDoc.WorkbookPart.WorksheetParts.First()),
        SheetId = 1,
        Name = "Sheet1"
    });
}

您可以查看$("#locked").click(function () { $(this).toggleClass("pulse-icon-unlocked"); $('select#signontype').prop('disabled', !$(this).hasClass("pulse-icon-unlocked")); }); 是否有课程#locked

演示:http://jsfiddle.net/tusharj/thhxcmso/4/

答案 1 :(得分:3)

您只需要检查锁定的div是否包含pulse-icon-unlocked.hasClass()

pulse-icon-unlocked

Fiddle Example

答案 2 :(得分:1)

您可以使用

$('select#signontype').is(':enabled')

检查选择框是否已启用。在此基础上,你可以做你想做的事。

Fiddle

答案 3 :(得分:0)

$("YourElementID").selectbox('disable')将停用selectpicker选择框。

注意:这适用于 selectbox版本0.2 。我不确定最新版本。