jQuery 如果输入文本等于 var 做某事
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18719533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery if input text equals var do something
提问by Sam Skirrow
I have a form that calculates the sum of several input values (checkboxes and dropdowns - not user entered values). At the bottom of the form I have a "Discount Code" box, I want to be able to enter a code and it takes a set amount off the total.
我有一个表格可以计算几个输入值的总和(复选框和下拉列表 - 不是用户输入的值)。在表格的底部,我有一个“折扣代码”框,我希望能够输入一个代码,然后从总数中扣除一定的金额。
With my code I am trying to say, if text entered = discount code, change the value of the input to 50. Here is what I have so far
使用我的代码,我想说,如果输入的文本 = 折扣代码,请将输入的值更改为 50。这是我目前所拥有的
var discountCode = 'DISTR50';
var codeEntered = $("input[name='discount']");
if (discountCode = codeEntered) {
$('#discount input').attr('value',50);
}
and for the html:
对于 html:
<input type="text" id="discount" name="discount" class="txt"/>
回答by sushil bharwani
if (discountCode == codeEntered) {
$('input#discount').attr('value',50);
}
Do not use = use either == or better ===
不要使用 = 使用 == 或更好的 ===
Also instead of using attr you can simple use val as suggested below and use input#discount
as the correct selector and use val() to get the value
此外,除了使用 attr,您还可以按照下面的建议简单地使用 val 并input#discount
用作正确的选择器并使用 val() 来获取值
var discountCode = 'DISTR50';
var codeEntered = $("input[name='discount']").val();
if (discountCode == codeEntered) {
$('input#discount').val('50');
}
回答by Jite
= is assignment, == or === is to check if something is equal to the other.
= 是赋值,== 或 === 是检查某物是否与另一个相等。
What is it you want to fetch from $("input[name='discount']")? Cause this will give you a jquery object, try $("input[name='dicount']").val(); to fetch value, ie something like:
你想从 $("input[name='discount']") 获取什么?因为这会给你一个 jquery 对象,试试 $("input[name='dicount']").val(); 获取价值,即类似:
if('DISTR50' == $("input[name='discount']").val()){
$("#discount input").attr("value", 50);
}
回答by Reinstate Monica Cellio
Just a couple of minor issues...
只是几个小问题...
var discountCode = 'DISTR50';
var codeEntered = $("#discount").val();
if (discountCode == codeEntered) {
$('#discount').val(50);
}
Notice I added .val()
to to get and set values of inputs, and changed the if statement to use ==
instead of =
.
请注意,我添加了.val()
to 来获取和设置输入的值,并将 if 语句更改为使用==
而不是=
.
I also used the ID of the input element, as this will be quicker than looking for an input by name.
我还使用了输入元素的 ID,因为这比按名称查找输入要快。
回答by Dhaval Marthak
You can't compare object with values
您不能将对象与值进行比较
You need to do like this:
你需要这样做:
var discountCode = 'DISTR50';
var codeEntered = $("input[name='discount']").val(); <-- .val()
The code you are writing returns object in codeEntered
variable, you should use .val()
to get the value of that object.
您正在编写的代码以codeEntered
变量形式返回对象,您应该使用它.val()
来获取该对象的值。
And for the comparision use ==
or ===
(for strict comparison)
并用于比较使用==
or ===
(用于严格比较)
回答by Bibhu
Try this
尝试这个
var discountCode = 'DISTR50';
var codeEntered = $("input[name='discount']");
if (discountCode === codeEntered) {
$('#discount').val('50');
}