简体   繁体   中英

Pass a form hidden field variable from one function to another

The second_scored function is called by an onblur event on the end_date. When a user completes the entry of the end_date, the value of the hidden form elements are modified with data from a query, in addition to other innerHTML elements (i have omitted those).

The other_scored function can be separately called by a user if they want to accept to accept or agree to the data displayed (now in hidden form element). They check the checkbox and data should be copied from the second_scored() function to the function other_scored() .

The problem is when I click on the checkbox in order to copy from hidden form element nothing is copied and it does not show.

function second_scored()
{
// { ... }
    document.getElementById('inspection_number').value = '65888';
    document.getElementById('inspection_date').value = '12/31/2007'; 
    document.getElementById('inspection_score').value = '90';
// { ... }
}

function other_scored()
{
  // { ... }
  if (document.getElementById('inspection_number'))
            {
                document.getElementById('b_inspection_number').value = document.getElementById('inspection_number').value;
            }

}

This is the form

FROM: <input name="begin_date" type="text" date" size="12" value="#" id="begin_date">
TO: <input name="end_date" type="text" date" size="12" value="#" id="end_date" onblur="second_score();">
<!-->
I agree to this score: <input type="checkbox" name="IagreePI" id="IagreePI" onclick="other_scored();">
<!-->

Your question title says

Pass a form hidden field variable from one function to another

So if you want to use a variable in two function a global variable can be used like,

var variable;
function second_scored()
{
    variable=65888;
}

function other_scored()
{
    console.log(variable);
}

Set the variable from one function and access it from another one.

But If you really want to set value of hidden fields and access them from another function check this Jsbin example .(I put a saperate button to execute other_scored() function since you haven't given your html for that)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM