我一直在尝试使用jQuery在表单中设置隐藏字段的值,但没有成功。

下面是解释该问题的示例代码。如果我保持输入类型为“文本”,它工作起来没有任何麻烦。但是,将输入类型更改为“hidden”,却不起作用!

<html>

    <head>
        <script type="text/javascript" src="jquery.js">
        </script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("button").click(function() {
                    $("input:text#texens").val("tinkumaster");
                });
            });
        </script>
    </head>

    <body>
        <p>
            Name:
            <input type="hidden" id="texens" name="user" value="texens" />
        </p>
        <button>
            Change value for the text field
        </button>
    </body>

</html>

我还尝试了以下解决方法,通过设置输入类型为“text”,然后为输入框使用“display:none”样式。但是,这也失败了! jQuery似乎在设置隐藏或不可见的输入字段时遇到了一些麻烦。

什么好主意吗?有没有可行的解决办法?


当前回答

Actually, this is an ongoing problem. While Andy is right about the downloaded source, .val(...) and .attr('value',...) don't seem to actually modify the html. I think this is a javascript problem and not a jquery problem. If you had used firebug even you would have had the same question. While it seems that if you submit the form with the modified values it will go through, the html does not change. I ran into this issue trying to create a print preview of the modified form (doing [form].html()) it copies everything okay, including all changes except values changes. Thus, my modal print preview is somewhat useless... my workaround may have to be to 'build' a hidden form containing the values they have added, or generate the preview independently and make each modification the user makes also modify the preview. Both are inoptimal, but unless someone figures out why the value-setting behavior does not change the html (in the DOM i'm assuming) it will have to do.

其他回答

如果你在设置一个隐藏字段的值时遇到了麻烦,因为你传递了一个ID给它,这是解决方案:

用$("input[id=hidden_field_id]").val(id)代替$("#hidden_field_id").val(id)。不知道有什么区别,但确实有效。

我也有同样的问题。奇怪的是谷歌铬和可能其他人(不确定)不喜欢

$("#thing").val(0);

input type="hidden" id="thing" name="thing" value="1" />

(没有变化)

$("#thing").val("0");

input type="hidden" id="thing" name="thing" value="1" />

(没有变化)

但这是可行的!!!!

$("#thing").val("no");

input type="hidden" id="thing" name="thing" value="no" />

改变! !

$("#thing").val("yes");

input type="hidden" id="thing" name="thing" value="yes" />

改变! !

一定是“字符串的东西”

Actually, this is an ongoing problem. While Andy is right about the downloaded source, .val(...) and .attr('value',...) don't seem to actually modify the html. I think this is a javascript problem and not a jquery problem. If you had used firebug even you would have had the same question. While it seems that if you submit the form with the modified values it will go through, the html does not change. I ran into this issue trying to create a print preview of the modified form (doing [form].html()) it copies everything okay, including all changes except values changes. Thus, my modal print preview is somewhat useless... my workaround may have to be to 'build' a hidden form containing the values they have added, or generate the preview independently and make each modification the user makes also modify the preview. Both are inoptimal, but unless someone figures out why the value-setting behavior does not change the html (in the DOM i'm assuming) it will have to do.

任何人谁是与此斗争,有一个非常简单的“内联”方法来做到这一点与jQuery:

<input type="search" placeholder="Search" value="" maxlength="256" id="q" name="q" style="width: 300px;" onChange="$('#ADSearch').attr('value', $('#q').attr('value'))"><input type="hidden" name="ADSearch" id="ADSearch" value="">

当使用onChange事件将文本输入可见输入时,设置隐藏字段的值。如果你想把字段值传递给“高级搜索”表单,而不是强迫用户重新输入他们的搜索查询,这是很有用的(就像在我的例子中)。

又有人在这里抓你了,浪费了我的时间,所以我想我应该把这个建议给你。我有一个隐藏字段,我给了一个id。名称中的[]括号(由于与struts2一起使用)和选择器$(“#model.thefield[0]”)不会找到我的隐藏字段。将id重命名为不使用句号和括号会导致选择器开始工作。因此,最后我以model_the_field_0的id代替,选择器工作正常。