Pass a javascript variable value into input type hidden value

后端 未结 8 1188
情歌与酒
情歌与酒 2020-11-27 11:38

I would like to assign value of product of two integer numbers into a hidden field already in the html document. I was thinking about getting the value of a javascript varia

相关标签:
8条回答
  • 2020-11-27 11:56

    if you already have that hidden input :

    function product(a, b) {
       return a * b;
    }
    function setInputValue(input_id, val) {
        document.getElementById(input_id).setAttribute('value', val);
    }
    

    if not, you can create one, add it to the body and then set it's value :

    function addInput(val) {
        var input = document.createElement('input');
        input.setAttribute('type', 'hidden');
        input.setAttribute('value', val);
        document.body.appendChild(input);
    }
    

    And then you can use(depending on the case) :

    addInput(product(2, 3)); // if you want to create the input
    // or
    setInputValue('input_id', product(2, 3)); 
    
    0 讨论(0)
  • 2020-11-27 11:58

    Check out this jQuery page for some interesting examples of how to play with the value attribute, and how to call it:

    http://api.jquery.com/val/

    Otherwise - if you want to use jQuery rather than javascript in passing variables to an input of any kind, use the following to set the value of the input on an event click(), submit() et al:

    on some event; assign or set the value of the input:

    $('#inputid').val($('#idB').text());
    

    where:

    <input id = "inputid" type = "hidden" />
    
    <div id = "idB">This text will be passed to the input</div>
    

    Using such an approach, make sure the html input does not already specify a value, or a disabled attribute, obviously.

    Beware the differences betwen .html() and .text() when dealing with html forms.

    0 讨论(0)
  • 2020-11-27 12:03

    add some id for an input

    var multi = product(2,3);
    document.getElementById('id').value=multi;
    
    0 讨论(0)
  • 2020-11-27 12:03
    <script type="text/javascript">
      function product(x,y)
      {
       return x*y;
      }
     document.getElementById('myvalue').value = product(x,y);
     </script>
    
     <input type="hidden" value="THE OUTPUT OF PRODUCT FUNCTION" id="myvalue">
    
    0 讨论(0)
  • 2020-11-27 12:04

    You could do that like this:

    <script type="text/javascript">
         function product(a,b)
         {
         return a*b;
         }
        document.getElementById('myvalue').value = product(a,b);
     </script>
    
     <input type="hidden" value="THE OUTPUT OF PRODUCT FUNCTION" id="myvalue">
    
    0 讨论(0)
  • 2020-11-27 12:07

    You could give your hidden field an id:

    <input type="hidden" id="myField" value="" />
    

    and then when you want to assign its value:

    document.getElementById('myField').value = product(2, 3);
    

    Make sure that you are performing this assignment after the DOM has been fully loaded, for example in the window.load event.

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