hidden form field not getting in a controller in s

2019-08-15 17:10发布

问题:

my intension is to send a hidden count value form view to controller in spring mvc,everything is working but iam not getting the count in the controller,plese do some favour my view is

<HTML>
<HEAD>

<script type="text/javascript" src="resources/js/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    var i = 1;
    //var c = 0;
    $("#AddSection").click(function() {
        i++;
        $("<div />").append($("<input />", {
            type: "text",
            name: "Section" + i
        })).appendTo("#someContainer");

        //c = i;
        document.getElementsByName("Count").Value = i;
        alert(i);
    });

    $("input").live("click", function() {
        $("span").text("Section: " + this.name);

    });
});​
</SCRIPT>
</HEAD>
<BODY>
    <form method="get" action="addProfile">
        ProfileName<input type="text" name="pname"><br />
         SectionName<input type="text" name="Section1">
          <input type="button" id="AddSection" value="AddSection">
        <div id="someContainer"></div>
    <input type="hidden" id="hiddenSection" name="Count" />
        <span></span> <input type="submit" value="save"> 
    </form>
</BODY>
</HTML>

回答1:

You have a typo:

document.getElementsByName("Count").Value = i;
//                                  ^----Typo!

value is lowercase:

document.getElementsByName("Count")[0].value = i;   
//                                     ^----Fixed!  
//                                 ^-------- return HTML Collection, take first.

Better use id:

document.getElementById("hiddenSection").value = i;     

You're using jQuery, so you can use one of the following:

$("#hiddenSection").val(i);
$('input[name="Count"]').val(i);