我一直在尝试使用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似乎在设置隐藏或不可见的输入字段时遇到了一些麻烦。

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


当前回答

使用val()不适合我。我必须在任何地方使用.attr()。此外,我有不同类型的输入,在复选框和选择菜单的情况下必须非常明确。

更新文本框

$('#model_name').attr('value',nameVal);

更新文件输入旁边的图像

$('#img-avatar').attr('src', avatarThumbUrl);

更新布尔

if (isActive) {
    $('#model_active').attr('checked',"checked");
} else {
    $('#model_active').attr('checked', null) ;
}

更新选择(包含数字或字符串)

$('#model_framerate').children().each(function(i, el){
    var v = $(el).val();
    if (v === String(framerateVal)) { 
        $(el).attr('selected','selected');
    } else {
        $(el).attr('selected',null);
    }
}

其他回答

在尝试了一切之后,devtools显示他们改变了id,这是有效的:

$("#form-field-{field_id}").value('free');

简单地使用get和set下面的语法

GET

//Script 
var a = $("#selectIndex").val();

这里一个变量将保存hiddenfield(selectindex)的值

服务器端

<asp:HiddenField ID="selectIndex" runat="server" Value="0" />

SET

var a =10;
$("#selectIndex").val(a);

使用val()不适合我。我必须在任何地方使用.attr()。此外,我有不同类型的输入,在复选框和选择菜单的情况下必须非常明确。

更新文本框

$('#model_name').attr('value',nameVal);

更新文件输入旁边的图像

$('#img-avatar').attr('src', avatarThumbUrl);

更新布尔

if (isActive) {
    $('#model_active').attr('checked',"checked");
} else {
    $('#model_active').attr('checked', null) ;
}

更新选择(包含数字或字符串)

$('#model_framerate').children().each(function(i, el){
    var v = $(el).val();
    if (v === String(framerateVal)) { 
        $(el).attr('selected','selected');
    } else {
        $(el).attr('selected',null);
    }
}

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

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

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.