最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Simple JS function Not Working

programmeradmin1浏览0评论

I have been trying to get a simple JS function to work for the longest while but its just not working. Console says "test is not a function.

echo '<input type="hidden" value="'.$return_survey_url.'" id="return_url">';
echo '<form action=".php" onsubmit="return test();" method="post" target="votar">';
echo '<textarea name="tweet" rows="8" cols="10" maxlength="130"></textarea>';
echo '<input type="submit" value="test"/>';
echo '<iframe name="votar" style="display:block; width: 500px; height: 500px;"></iframe>';    
echo '</form>';

The below that I close the PHP tag and have a simple script:

 <script type="text/javascript">
      function test() {
        alert('thank you');
         var return_url = document.getElementById('return_url').value;
         window.location.href= return_url;

     }
  </script>

Whenever I click the submit button console spits out that it is not a function. I am trying to get this to work within a CPT template, this code only needs to happen on that specific template. This same code works here:

/

I have been trying to get a simple JS function to work for the longest while but its just not working. Console says "test is not a function.

echo '<input type="hidden" value="'.$return_survey_url.'" id="return_url">';
echo '<form action="http://www.example/test.php" onsubmit="return test();" method="post" target="votar">';
echo '<textarea name="tweet" rows="8" cols="10" maxlength="130"></textarea>';
echo '<input type="submit" value="test"/>';
echo '<iframe name="votar" style="display:block; width: 500px; height: 500px;"></iframe>';    
echo '</form>';

The below that I close the PHP tag and have a simple script:

 <script type="text/javascript">
      function test() {
        alert('thank you');
         var return_url = document.getElementById('return_url').value;
         window.location.href= return_url;

     }
  </script>

Whenever I click the submit button console spits out that it is not a function. I am trying to get this to work within a CPT template, this code only needs to happen on that specific template. This same code works here:

https://jsfiddle/n7zd1hw8/

Share Improve this question asked Feb 14, 2016 at 18:10 Uriahs VictorUriahs Victor 3854 silver badges15 bronze badges 2
  • your code works for me with a copy/paste into a template file, you'll have to give some more context as to where you've got this code exactly. – Milo Commented Feb 14, 2016 at 18:18
  • As soon as I click the button console tells me that test is not a function pastebin/VTU9eDMZ – Uriahs Victor Commented Feb 14, 2016 at 21:32
Add a comment  | 

1 Answer 1

Reset to default 1

Added ID to form submit botton and removed onSubmit event:

echo '<input type="hidden" value="'.$return_survey_url.'" id="return_url">';
echo '<form action="http://www.example/test.php" method="post" target="votar">';
echo '<textarea name="tweet" rows="8" cols="10" maxlength="130"></textarea>';
echo '<input type="submit" value="test" id="test"/>';
echo '<iframe name="votar" style="display:none;"></iframe>';    
echo '</form>';

Added JS in noconflict mode and called function when submit button click:

  <script type="text/javascript">
  jQuery(function($) {
    // Code that uses jQuery's $ can follow here.
    $( "#test" ).click(function() {
      alert('some message');
      var return_url = document.getElementById('return_url').value;
      window.location.href= return_url;
    });

});
</script>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论