gpt4 book ai didi

javascript - Bootstrap 导航栏搜索表单输入具有未应用的错误类

转载 作者:行者123 更新时间:2023-11-28 04:26:35 25 4
gpt4 key购买 nike

因此,我可以使用 JavaScript 以我想要的方式控制表单(至少在表单提交功能方面),并且此代码笔上有一个事件的工作表单。但是,当表单“无效”时,我无法将 Bootstrap 输入类“has-error”应用到此表单并将输入周围的颜色变成红色。可以在此处找到有关“has-error”类的更多信息:http://getbootstrap.com/css/#forms-control-validation

https://codepen.io/MaineCannabisKid/pen/ZymzJO

这是表单代码

<form class="navbar-form navbar-nav search-form" method="get" action="search.php" name="searchForm" id="searchForm">
<!-- hidden type input -->
<input type="hidden" name="searchType" id="searchType" value="users" required>
<div class="input-group">
<input type="text" id="searchInput" name="searchInput" class="form-control" aria-label="Search the site" placeholder="Search Users">
<div class="input-group-btn">
<button type="button" class="btn btn-default" id="searchFormSubmit">Search</button>
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><a href="#" id="searchForm-users">Users</a></li>
<li><a href="#" id="searchForm-another">Another Search</a></li>
</ul>
</div>
</div>
</form>

这是 javascript

console.log("Navbar.js Connected");

// Search Form Validate Function
function searchFormValidate() {
// Check is Search Input value is null, if so return false (Validation didn't pass)
return $('#searchInput').val() == "" ? false : true;
}

// When Search Users is clicked on
$('#searchForm-users').on('click', function() {
$('#searchType').val('users');
$('#searchInput').attr("placeholder", "Search Users");
});

// When Another Search is clicked on
$('#searchForm-another').on('click', function() {
$('#searchType').val('another');
$('#searchInput').attr("placeholder", "Search Something Else");
});

// When Search is clicked Submit Form
$("#searchFormSubmit").on('click', function() {
if(searchFormValidate()) {
document.searchForm.submit();
} else {
// Right here is where the form should apply the has-error class
console.log("Validation Error");
}
});

最佳答案

这将解决您的问题。

$("#searchInput").parent().addClass("has-error");

注意:输入周围的线显示为红色,但由于背景颜色,肉眼看不到它。

console.log("Navbar.js Connected");

// Search Form Validate Function
function searchFormValidate() {
// Check is Search Input value is null, if so return false (Validation didn't pass)
return $('#searchInput').val() == "" ? false : true;
}

// When Search Users is clicked on
$('#searchForm-users').on('click', function() {
$('#searchType').val('users');
$('#searchInput').attr("placeholder", "Search Users");
});

// When Another Search is clicked on
$('#searchForm-another').on('click', function() {
$('#searchType').val('another');
$('#searchInput').attr("placeholder", "Search Something Else");
});

// When Search is clicked Submit Form
$("#searchFormSubmit").on('click', function() {
if (searchFormValidate()) {
// form valid submit form
document.searchForm.submit();
} else {
// form isn't valid, turn has-error on
console.log("Validation Error");
$("#searchInput").parent().addClass("has-error");
}
});
/* Navbar */

.navbar-default {
margin: 0 auto;
border-radius: 0;
z-index: 100;
background-color: transparent!important;
border-color: transparent!important;
}


/* Navbar Search Form */

.search-form .btn-default {
background: #ac3911;
border-color: rgba(149, 48, 15, 1);
}

.navbar-default .navbar-collapse,
.navbar-default .navbar-form {
border-color: transparent!important;
}


/* Drop Down Button */

.search-form .btn-default.dropdown-toggle {
border-left: 1px solid rgba(149, 48, 15, 1);
}


/* End Navbar Search*/
<link href="https://bootswatch.com/united/bootstrap.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-nav-demo" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="<?php echo Config::get('links/app_root'); ?>" class="navbar-brand hvr-grow">真 Makoto</a>
</div>
<div class="collapse navbar-collapse" id="bs-nav-demo">

<ul class="nav navbar-nav navbar-right">
<form class="navbar-form navbar-nav search-form" method="get" action="#" name="searchForm" id="searchForm">
<!-- hidden type input -->
<input type="hidden" name="searchType" id="searchType" value="users" required>
<div class="input-group">
<input type="text" id="searchInput" name="searchInput" class="form-control" aria-label="Search the site" placeholder="Search Users">
<div class="input-group-btn">
<button type="button" class="btn btn-default" id="searchFormSubmit">Search</button>
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><a href="#" id="searchForm-users">Users</a></li>
<li><a href="#" id="searchForm-another">Another Search</a></li>
</ul>
</div>
</div>
</form>


</ul>
</div>
</div>
</nav>

关于javascript - Bootstrap 导航栏搜索表单输入具有未应用的错误类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45003527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com