如何在javascript中为隐藏变量赋值空值? [英] How to assign null value to Hidden variable in javascript?
问题描述
我有一个名为str的隐藏输入变量。
I have a hidden input variable called str.
我正在为它分配abc值。
I am assigning "abc" value to it.
然后我尝试分配空值或者让我们说空引用。但我不能。
Then I try to assign null value or let's say null reference to it. But I couldn't.
编辑
代码的一部分。
隐藏字段...
<input id="str" name="str" type="hidden" value="" />
我也使用jQuery。
I also use jQuery.
if ($(str).val() == "abc") {
$("#str").val(null);
}
推荐答案
我不确定是否归零值是有意义的 - 您应该将值清空,或删除整个字段(而不仅仅是值)。
I'm not sure nulling the value is meaningful - you should either blank the value, or delete the whole field (not just the value).
根据您提供的示例代码...
Based on the example code you provided...
例如:
$("#str").val('')
或
$("#str").remove()
另一种选择,如果你可能需要打开或关闭字段(所以而不是删除和重新创建)将禁用字段 - 禁用字段没有提交表格。
Another option, if you may need to toggle the field on or off (so rather than deleting & re-creating) would be disabling the field - disabled fields don't get submitted with the form.
$("#str").attr('disabled','disabled')
and
$("#str").removeAttr('disabled')
这篇关于如何在javascript中为隐藏变量赋值空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!