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

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


当前回答

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

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

其他回答

$('input[name=hidden_field_name]').val('newVal');

为我工作,其实两者都不是

$('input[id=hidden_field_id]').val('newVal');

nor

$('#hidden_field_id').val('newVal');

did.

.val没有为我工作,因为我抓取值属性服务器端和值并不总是更新。所以我用了:

var counter = 0;
$('a.myClickableLink').click(function(event){
   event.preventDefault();
   counter++;

   ...
   $('#myInput').attr('value', counter);
}

希望它能帮助到别人。

:text对于type值为hidden的输入将失败。只使用以下语句也会更有效率:

$("#texens").val("tinkumaster");

ID属性在网页上应该是唯一的,确保你的ID属性是唯一的,因为这可能会导致你遇到的任何问题,指定一个更复杂的选择器只会减慢速度,看起来不那么整洁。

http://jsbin.com/elovo/edit上的示例,使用http://jsbin.com/elovo/2/edit上的示例代码

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.

<javascript>


slots=''; hidden=''; basket = 0;

    cost_per_slot = $("#cost_per_slot").val();
    //cost_per_slot = parseFloat(cost_per_slot).toFixed(2)

    for (i=0; i< check_array.length; i++) {
        slots += check_array[i] + '\r\n';
        hidden += check_array[i].substring(0, 8) + '|';
        basket = (basket + parseFloat(cost_per_slot));
    }

    // Populate the Selected Slots section
    $("#selected_slots").html(slots);

    // Update hidden slots_booked form element with booked slots
    $("#slots_booked").val(hidden);     

    // Update basket total box
    basket = basket.toFixed(2);
    $("#total").html(basket);