开发者

javascript xslt will not correctly post data

开发者 https://www.devze.com 2023-03-27 15:57 出处:网络
I am trying to submit a form using javascript (generated from an xslt stylesheet). <form name=\'myform\' action=\'search.php\' method=\'post\'>

I am trying to submit a form using javascript (generated from an xslt stylesheet).

<form name='myform' action='search.php' method='post'>
    <input type='hidden' name='query' />
    <script type='text/javascript'>
    function submit(id)
    {
      document.myform.elements[0] = id;  *Note* I have tried document.getElementById('query').value = id;
      document.myform.submit();
    }
    </script>
</form>

After this code I have xsl translations formatting xml data. I call the submit function like this: NOTE This href='' does nothing submit('somequery')

When I click on the href the javascript function executes and seems to work fine, except NO data gets POST'ed. I set the value to 'somequery' but when the form get开发者_Go百科s POST'ed, the value is '' (blank).

Why does it do this? I have tried createElement('input') and such from within javascript but I cannot ever get the form to POST the input value.


Are you using Firefox here? The behaviour may differ between browser. Firstly, you should go back to using getElementById

document.getElementById('query').value = id; 

The reason this may not have worked is that your hidden element does not actually specify an id attribute.

<input type='hidden' name='query' id='query' /> 

In IE, I believe it allows a lack of id and assumes it is equal to the name. In Firefox, a lack of id results in a javascript error when you try to do getElementById

<form name='myform' action='search.php' method='post'> 
<input type='hidden' name='query' id='query' /> 
<script type='text/javascript'> 
function submit(id) 
{ 
   document.getElementById('query').value = id; 
   document.myform.submit(); 
} 
</script> 
</form> 
0

精彩评论

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

关注公众号