Setting a form variable value before submitting

后端 未结 4 2001
旧时难觅i
旧时难觅i 2020-12-10 16:01

I have a page where you can view a hotel\'s information. On this page is a little form to search for room availability for the hotel page you are on.



        
相关标签:
4条回答
  • 2020-12-10 16:13

    You can hook the click event on btnHotelSearchAll and then fill in the value:

    document.getElementById("btnHotelSearchAll").onclick = function() {
        document.getElementById("Hotel").value = "0";
    };
    

    Be absolutely certain there's nothing else on the page that has either the id or name "Hotel", and that you don't have any global variables you've declared with that name, because some versions of IE have bugs where they conflate name values and global variable names into the namespace they use for document.getElementById. Or, alternately, make the id on the hidden field a bit more unique (the name can stay as it is so you don't have to change the backend; the id is only client-side, the name is what's sent to the server). E.g., you can do this:

    <input type="hidden" id="HotelField" name="Hotel" value="<%= HotelID %>">
                                  ^
    

    and then change the code a bit:

    document.getElementById("btnHotelSearchAll").onclick = function() {
        document.getElementById("HotelField").value = "0";
        //                            ^
    };
    

    Update:

    Note that the code to hook up the button must run after the button has been put in the DOM. So with the code above, that means making sure that the script block is below the form in the page, like this:

    <form ...>
    ....
    </form>
    ...
    <script>
    ...
    </script>
    

    If the script block is above the button, then the button won't exist yet as of when the script runs. This is one reason why it's frequently best to put scripts at the end of the body element, just before the closing </body> tag (more here).

    If you really want the script above the button, you have to delay the call by making it an onload event handler or that sort of thing. But window.onload happens very late in the process of a page load (it waits for all images and other assets to load, for instance), long after your users may be interacting with your form, so usually best to do it sooner.


    Off-topic: My standard note that a lot of this stuff is made earlier and more robust by using a decent JavaScript library like jQuery, Prototype, YUI, Closure, or any of several others. jQuery, for instance, will deal with the IE bugs in document.getElementById for you so you don't have to worry about the conflation problem.

    0 讨论(0)
  • 2020-12-10 16:13

    using onclick attribute to call javascript function that will set the hidden field value

     <input type="submit" name="btnHotelSearchAll" value="Search All Hotels" onclick="SetHotelID();">
    
    <script>
       function SetHotelID()
       {
          $('#Hotel').val('0');
       }
    </script>
    

    Note I am using Jquery

    0 讨论(0)
  • 2020-12-10 16:18

    If you'd like to use jQuery, try the following.

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" ></script>
    
    <script type="text/javascript" >
    $(function(){
      $('#btnHotelSearch').click(function(){
        $('#Hotel').val(0);
      });
    });
    </script>
    
    0 讨论(0)
  • 2020-12-10 16:36

    Here you go, let me know if this works...

    <form id="form1" name="form1" action="" method="POST">
    <input type="hidden" id="Hotel" name="Hotel" value="<%= HotelID %>">
    
    Arrive: <input value="<% strURLBookingDate %>" type="text" id="ArrivalDate" name="ArrivalDate">                            
    Depart: <input value="<% strURLBookingDate2 %>" type="text" id="DepartureDate" name="DepartureDate">
    
    <input type="submit" name="btnHotelSearch" value="Search This Hotel">
    <input type="submit" name="btnHotelSearchAll" value="Search All Hotels" onclick='document.getElementById("Hotel").value="0"'>
    

    0 讨论(0)
提交回复
热议问题