简体   繁体   English

如何在javascript中为隐藏变量赋值空值?

[英]How to assign null value to Hidden variable in javascript?

I have a hidden input variable called str. 我有一个名为str的隐藏输入变量。

I am assigning "abc" value to it. 我正在为它分配“abc”值。

Then I try to assign null value or let's say null reference to it. 然后我尝试分配空值或让我们说空引用。 But I couldn't. 但我不能。

Edit 编辑

part of code. 代码的一部分。

Hidden Field... 隐藏的领域......

<input id="str" name="str" type="hidden" value="" />

I also use jQuery. 我也使用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). 我不确定null值是否有意义 - 您应该将值清空,或者删除整个字段(而不仅仅是值)。

Based on the example code you provided... 根据您提供的示例代码...

For example: 例如:

$("#str").val('')

or 要么

$("#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')

Assign the empty string to it. 将空字符串分配给它。 It will be treated the same way on the server side. 它将在服务器端以相同的方式处理。

 var inp = document.getElementById('str');
 inp.value = '';  // actually inp.value = null will work here

Or using jQuery 或者使用jQuery

 if ($(str).val() == "abc")
 {
     $("#str").val('');
 }

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

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