- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我正在尝试对我的表单进行验证,并在折叠元素(该值的)= False 时将通过表单发送的值更改为空值(我仍然对 javascript 和 Jquery 有点陌生)。
if (document.getElementById("filter_form")) {
var slider1 = document.getElementById("price_min");
var slider2 = document.getElementById("price_max");
output1.value = slider1.value; // Display the default slider value
output2.value = slider2.value;
output1.oninput = function() {
slider1.value = this.value;
}
output2.oninput = function() {
slider2.value = this.value;
}
// Update the current slider value (each time you drag the slider handle)
slider1.oninput = function() {
output1.value = this.value;
}
slider2.oninput = function() {
output2.value = this.value;
}
}
$(document).ready(function() {
$('#search').click(function(event) {
if ($('#price').attr("aria-expanded") == "true") {
var price_min = Number($('#price_min').val());
var price_max = Number($('#price_max').val());
} else {
var price_min = null;
var price_max = null;
$('#price_min').value = null;
$('#price_max').value = null;
}
if (price_min > price_max) {
event.preventDefault();
iziToast.warning({
title: 'Fail!',
message: 'min price cant be larger than max price',
position: 'bottomRight'
}); //my alert
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/izitoast/1.4.0/js/iziToast.min.js"></script>
<form method="POST" action="/search_url/'">
<div class="form-group">
<p>
<button id="price" class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample-1" aria-expanded="false" aria-controls="collapseExample-1" style="width: 100%">
Price
</button>
</p>
<div class="collapse" id="collapseExample-1">
<div>Min: <input type="number" id="price_min_value" min="0" max="100000000000" value="0" /></div>
<input type="range" id="price_min" class="form-control" min="0" max="100000000000" value="0" name="price_min">
<div>Max: <input type="number" id="price_max_value" min="0" max="100000000000" value="100000000000" /></div>
<input type="range" id="price_max" class="form-control" min="0" max="100000000000" value="100000000000" name="price_max">
</div>
</div>
<button class="btn btn-primary mr-1" id="search">Search</button>
</form>
在服务器上,当我收到带有折叠flase的POST请求的数据时,我得到price_min ='0'和price_max ='100000000000'
预期结果:
应该是price_min = null并且price_max = null
我不知道为什么它不将 POST 数据设置为 NULL,我也不知道为什么。
编辑:
好吧,当我将设置值代码更改为:
$('#price_min').val("");
$('#price_max').val("");
并发送表单后,它返回服务器到等于范围输入的中间值(50000000000)的值,但是当我尝试这样做时:
$('#price_min').val("1234");
$('#price_max').val("12345");
在服务器上我实际上得到了price_min = 1234和price_max = 12345的变化值
为什么会这样?但是当我尝试将其设置为值或 null 或无时,它不起作用,只取范围输入的中间值?
我很困惑
感谢您的阅读
最佳答案
您正在使用.value
设置值,但在jQuery中我们使用.val()
并将值设置为0而不是null,因为数字类型输入不接受null。更改服务器端的逻辑以检查值是否为 0 而不是 null。
此外,使用 preventDefault
作为搜索按钮单击的第一行,这样按钮就不会直接提交表单。
见下面的代码
$(document).ready(function() {
$('#search').click(function(event) {
event.preventDefault(); // it will prevent default behavior of button clicked
console.log('button clicked');
if ($('#price').attr("aria-expanded") == "true") {
var price_min = new Number($('#price_min').val());
var price_max = new Number($('#price_max').val());
} else {
var price_min = 0;
var price_max = 0;
$('#price_min').val(price_min);
$('#price_max').val(price_max);
}
if (price_min > price_max) {
iziToast.warning({
title: 'Fail!',
message: 'min price cant be larger than max price',
position: 'bottomRight'
}); //my alert
} else {
$("#searchForm").submit();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/izitoast/1.4.0/js/iziToast.min.js"></script>
<form method="POST" action="/search_url/'" id="searchForm">
<div class="form-group">
<p>
<button id="price" class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample-1" aria-expanded="false" aria-controls="collapseExample-1" style="width: 100%">
Price
</button>
</p>
<div class="collapse" id="collapseExample-1">
<div>Min: <input type="number" id="price_min_value" min="0" max="100000000000" value="0" /></div>
<input type="range" id="price_min" class="form-control" min="0" max="100000000000" value="0" name="price_min">
<div>Max: <input type="number" id="price_max_value" min="0" max="100000000000" value="100000000000" /></div>
<input type="range" id="price_max" class="form-control" min="0" max="100000000000" value="100000000000" name="price_max">
</div>
</div>
<button class="btn btn-primary mr-1" id="search">Search</button>
</form>
关于javascript - 如果 crash = false,Jquery 发送表单 POST 更改输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58263489/
class test { public static void main(String[] args){ Object o1 = new Object(); O
我以为我理解了 Python 中的这两个单例值,直到我看到有人在代码中使用 return l1 or l2,其中 l1 和 l2 都是链表对象,并且(s)他想如果不为 None 则返回 l1,否则返回
这个问题在这里已经有了答案: Why does the expression 0 >> (True == False) is False True >>> True == (False is Fals
为什么在 Python 中它是这样评估的: >>> False is False is False True 但是当用括号尝试时表现如预期: >>> (False is False) is False
我有一个名为“apple”的表,我编写了以下查询: select name, count(name), case when istasty is null then fal
python boolean 逻辑中的运算符优先级 print(False==True or False) #answer is True print(False==(False or True))#
请不要看条件,因为它们在这里是为了便于理解行为 为什么 result 等于 true ? boolean result = false && (false)?false:true; 我知道我们可以通过
乍一看,这篇文章可能看起来像是重复的,但事实并非如此。相信我,我已经查看了所有 Stack Overflow,但都无济于事。 无论如何,我从 Html.CheckBoxFor 得到了一些奇怪的行为。
这个问题在这里已经有了答案: python operator precedence of in and comparison (4 个答案) 关闭 6 年前。 我的一位前辈演示了它,我想知道这是否是
我最近参加了 Java 的入门测试,这个问题让我很困惑。完整的问题是: boolean b1 = true; boolean b2 = false; if (b2 != b1 != b2) S
为什么 {} == false 评估为 false 而 [] == false 评估为 true在 javascript 中? 最佳答案 这是根据 Abstract Equality Comparis
这个问题在这里已经有了答案: Why does (1 in [1,0] == True) evaluate to False? (1 个回答) 关闭7年前。 为什么使用括号时这些语句按预期工作: >>
我试过搜索这个,但我真的不知道如何表达它以查看是否有其他人发布了答案。 但是,我正在制作一个国际象棋游戏和一个人工智能来配合它,这是非常困难的,我的问题是当我检查两个棋子是否在同一个团队时我必须做 (
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
为什么 为 false || null 返回与 null || 不同的结果错误? 我可以安全地依赖 return myVar || false 如果 myVar 为 null 或 false,则返回
我正在尝试遵循 NHibernate 教程,“你的第一个基于 NHibernate 的应用程序:修订 #4”在 NHibernate Forge。 但线路:new SchemaExport(cfg).
这个问题在这里已经有了答案: Empty list boolean value (3 个答案) 关闭 4 年前。 我是 Python 的新手,不理解以下行为: 为什么要声明 [] == False
以下函数循环访问对象的值。如果值为空this.hasInvalidValue设置为true ,如果不为空 this.hasInvalidValue设置为false : user: { email:
所以我正在玩 java.lang.reflect 东西并尝试制作类似 this 的东西。这是我的问题(可能是一个错误): 将字段设置为 true 的方法的代码: private static void
当我在编程时,我的 if 语句出现了意想不到的结果。 这个代码警报怎么会是真的?我在 W3S 没有找到任何可以帮助我的东西,我真的很想知道为什么这些警报是“正确的” window.alert(fals
我是一名优秀的程序员,十分优秀!