开发者

return js value in HTML

开发者 https://www.devze.com 2023-01-13 18:07 出处:网络
First tentative steps into client side. I\'m having trouble finishing the following. My question is how do I return the value of a function in a HTML statement ...

First tentative steps into client side. I'm having trouble finishing the following. My question is how do I return the value of a function in a HTML statement ...

<script language="Javascript">
function checkjava()
{
 return 1
}
</script>

</head>
<body>
<form action="enabled_catch.php" method="get" name="your_form">
<input type="HIDDEN" name="answer" value="RETURN checkjava() HERE")>
<input type="s开发者_Python百科ubmit" value="click me">
</form>
</body>

I'd appreciate your help Thanks in advance

G


<head>
  <script language="Javascript">
    function checkjava() {
      return 1
    }
  </script>
</head>
<body>
  <form action="enabled_catch.php" 
        method="get" 
        name="your_form"
        onsubmit="this.answer.value=checkjava();">
    <input type="hidden" name="answer">
    <input type="submit" value="click me">
  </form>
</body>

No need for the id attribute.


<form action="enabled_catch.php" method="get" name="your_form" >
<input type="HIDDEN" name="answer" value="RETURN checkjava() HERE")>
<input type="submit" onclick="document.getElementById('answer').value=checkjava();"  value="click me">


You would do something like that:

<script language="Javascript">
  function checkjava()
  {
   return 1
  }
</script>

</head>
<body>
 <form action="enabled_catch.php" method="get" name="your_form" onsubmit="document.getElementById('answer').value=checkjava();">
  <input type="HIDDEN" id="answer" >
  <input type="submit" value="click me">
 </form>
</body>

Basically on form submit you would set the value of the answer input to the result of the function call.

EDIT: placed onsubmit on the wrong element before (embarrassing).


I would have an id on the answer tag and then do it in the form onsubmit event.

Something like:

<form onsubmit="document.getElementById("answerId").value = checkJava()" action="enabled_catch.php" method="get" name="your_form">
<input type="HIDDEN" name="answer" id="answerId" value="RETURN checkjava() HERE")>
<input type="submit" value="click me">
</form>


Browser-side scripting is event driven. Unless an event is triggered, nothing happens.

You could listen to the click event on your submit button, and then manipulate the value of your hidden field:

<input type="hidden" id="answer" name="answer" value="")>
<input type="submit" value="click me" 
       onclick="document.getElementById('answer').value = checkjava();">

Note how we had to give an id to the hidden field, in order to be able to reference it with getElementById().

Also note that JavaScript is case sensitive: checkjava() and checkJava() are not the same.


<script>
document.your_form.answer.value = checkjava();
</script>

Unless you want to use a fancy JS library. =)

0

精彩评论

暂无评论...
验证码 换一张
取 消