如何在选择bootstrap multiselect ckeckbox时发出警报?

时间:2017-10-25 06:23:29

标签: jquery bootstrap-multiselect

我正在使用bootstrap multiselect。我想在选中特定复选框时生成alert

我的html如下所示

<label>Particulars :</label>
    <select id="example-getting-started" multiple="multiple">
        <option value="DS Form">DS Form</option>
        <option class="proof" value="Proofs">Proofs</option>
        <option value="Banners">Banners</option>
        <option value="IRCTC Form">IRCTC Form</option>
        <option value="R3 Form Returned">R3 Form Returned</option>
        <option value="Declaration Form">Declaration Form</option>
        <option value="Agent Declaration Form">Sub Agent Declaration Form</option>
        <option value="Old Tokens">Old Tokens</option>
        <option value="11">Registration Form </option>
        <option value="12">12</option> 
</select>

我想在选中“校样”复选框时生成警报。

我试过下面的代码

 if( $('.multiselect-container>li>a>label>input[type=checkbox][value="proofs"]').prop("checked") == true)){
        alert("proofs checked");
    }

但它不起作用可以帮助我。

2 个答案:

答案 0 :(得分:1)

您可以使用onChange bootstrap multiselect事件获取所选选项。如您的问题所述,以下代码仅检查Proofs并提醒用户。

$('#example-getting-started').multiselect({
    onChange: function (element, checked) {
	if (checked === true) {
	   if ($(element).val() == "Proofs")  alert("proofs checked");
	}
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.min.js"></script>

<label>Particulars :</label>
<select id="example-getting-started" multiple="multiple">
	<option value="DS Form">DS Form</option>
	<option class="proof" value="Proofs">Proofs</option>
	<option value="Banners">Banners</option>
	<option value="IRCTC Form">IRCTC Form</option>
	<option value="R3 Form Returned">R3 Form Returned</option>
	<option value="Declaration Form">Declaration Form</option>
	<option value="Agent Declaration Form">Sub Agent Declaration Form</option>
	<option value="Old Tokens">Old Tokens</option>
	<option value="11">Registration Form </option>
	<option value="12">12</option>
</select>

答案 1 :(得分:0)

试试这个:

$('#checkboxid').change(function() {
    if ($('#checkboxid').prop('checked')) {
        //checked - insert code here
    }
    else {
        //not checked - insert code here
    }
});