简体   繁体   English

Bootstrap JQuery:对所选项目的下拉菜单验证

[英]Bootstrap JQuery: dropdown menu validation on selected item

I have the following twitter bootstrap dropdown menu which is part of a form: 我有以下twitter引导程序下拉菜单,它是表单的一部分:

<div class="form-group">
    <label for="platform" class="control-label col-xs-2">Platform:</label>
    <div class="col-xs-10">
        <div class="dropdown">
            <button class="btn btn-default" id="dropdownMenu2" data-toggle="dropdown">
                <span id="dropdown_title2">Select</span>
                <span class="caret"></span>
            </button>
            <ul class="dropdown-menu" id="dropdownMenu2">
                <li><a tabindex="-1" href="#">Android</a></li>
                <li><a tabindex="-1" href="#">IOS</a></li>
                <li><a tabindex="-1" href="#">Windows Phone</a></li>
            </ul>
        </div>
    </div>
</div>

once the page is loaded then when a user selects an item i use this 一旦页面被加载,那么当用户选择一个项目时,我使用它

var platform = ("Platform: " + $("#dropdown_title2").text());
$('#printPlatform').html(platform);

to set the dropdown value to a var and then display it within a modal using 将下拉列表值设置为var,然后使用

<span id="printPlatform"></span><br />

I'm now looking to validate this dropdown to check an item has been selected and the default value of "select" has been changed. 我现在正在寻找验证此下拉列表的方法,以检查是否已选择一项,并且默认值“ select”已更改。

Here's what I tried: 这是我尝试过的:

var selected = "Select"
if ($("#dropdown_title1").text == selected) {
    alert('Please fill in missing details');
}

However, when the code is run no alert dialog is displayed. 但是,运行代码时,不会显示警告对话框。 Anyone know where I'm going wrong? 有人知道我要去哪里错吗?

Current Validation for textboxs (drop down validation will be added to this method) 文本框的当前验证(下拉验证将添加到此方法)

$(document).ready(function () {
    @*Validation for Empty fields with name formpart*@
    $('#SendRequest').click(function (e) {
        var isValid = true;            
        $("[name='formpart']").each(function () {
            if ($.trim($(this).val()) == '') {
                isValid = false;
                $(this).css({
                    "border": "1px solid red",
                    "background": "#FFCECE"
                });
            }
            else {
                $(this).css({
                    "border": "",
                    "background": ""
                });
                $("#basicModal").modal('show');
            }
        });
        if (isValid == false)
            alert('Please fill in missing details');
        else
            alert('Thank you for submitting');
    });

It seems like you're confusing a the dropdown control in Bootstrap with a Select element. 似乎您将Bootstrap中的dropdown控件与Select元素混淆了。 The dropdown can be manually configured to behave as a select control, but you'll need to do it yourself. 可以手动将下拉列表配置为充当选择控件,但是您需要自己进行操作。

For starters, you'll want to do any manipulations whenever the links in the dropdown-menu are clicked. 对于初学者,每当单击dropdown-menu中的链接时,您都将需要进行任何操作。 You can do that with a delegate event handler like this: 您可以使用如下的委托事件处理程序来做到这一点:

$(".dropdown").on("click", "li a", function() {
    // Handle Clicks Here
});  

Once in there, you can get the text of the currently select anchor like this: 进入该目录后,您可以像这样获取当前选择的锚文本:

var platform = $(this).text();

Then apply that to whatever other controls you'd like to store that information. 然后将其应用于您想要存储该信息的任何其他控件。 If you don't persist it in some way, the dropdown control will have no memory of that option being previously selected . 如果您不以某种方式保留它,则下拉控件将​​不存储先前选择的选项。 You can add it to the dropdown menu button or to another text field like this: 您可以将其添加到下拉菜单按钮或其他文本字段中,如下所示:

$("#dropdown_title2").html(platform);
$('#printPlatform').html(platform);

Here's the whole thing in jsFiddle 这是jsFiddle中的全部内容

Update: 更新:

If you want to check the value on validation, just check any of the places into which you have persisted the value. 如果要在验证时检查值,只需检查将值保留在其中的任何位置。 If you added it to the screen, you can check it there. 如果将其添加到屏幕,则可以在此处进行检查。 If you don't need it to appear anywhere, then you can add it to the dropdown menu as a data attribute. 如果您不需要它出现在任何地方,则可以将其作为数据属性添加到下拉菜单中。 Here's one way you could do that now: 这是您现在可以执行的一种方法:

$("#SendRequest").click(function() {

    var platform = $("#dropdown_title2").html();
    var isValid = (platform !== 'Select')

    if (!isValid) {
        alert('Please fill in missing details');
    } else {
        alert('Thank you for submitting');
    }
});

If you need something more specific, I'd recommend using this fiddle as a starter template and getting a working example that reproduces the exact issue you're having 如果您需要更具体的内容,建议您使用此小提琴作为入门模板,并获得一个可以重现您遇到的确切问题的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM