Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“两个javascript”;行动“;在两个不同页面中的一个函数内_Javascript_Php - Fatal编程技术网

“两个javascript”;行动“;在两个不同页面中的一个函数内

“两个javascript”;行动“;在两个不同页面中的一个函数内,javascript,php,Javascript,Php,我试图在custom.js文件中使用以下代码修改几个SelectBox。每个ID位于不同的页面中,我在footer.php中使用的代码如下: <script> var selectFunction = function() { document.getElementById('ofae').options[0].text = 'Artists'; }; var selectFunctionn = function() { document.ge

我试图在custom.js文件中使用以下代码修改几个SelectBox。每个ID位于不同的页面中,我在footer.php中使用的代码如下:

<script>
    var selectFunction = function() {
    document.getElementById('ofae').options[0].text = 'Artists';
    };
    var selectFunctionn = function() {
    document.getElementById('ofav').options[0].text = 'Artists';
    }; 
</script>

var selectFunction=function(){
document.getElementById('ofae')。选项[0]。文本='Artists';
};
var selectFunctionn=函数(){
document.getElementById('ofav')。选项[0]。文本='Artists';
}; 
然后:



问题是,只有其中一个被执行,当我在第2页时,第一个值为NULL,它不会执行其余的代码。

测试文件test.php包含以下代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Test php echo script</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    </head>
    <body>
        <select id="ofav">
            <option value="">o1</option>
            <option value="">o2</option>
            <option value="">o3</option>
            <option value="">o4</option>
            <option value="">o5</option>
        </select>
        <select id="ofae">
            <option value="">o1</option>
            <option value="">o2</option>
            <option value="">o3</option>
            <option value="">o4</option>
            <option value="">o5</option>
        </select>
        <script>
        var selectFunction = function() {
        document.getElementById('ofav').options[0].text = 'Artists';
        document.getElementById('ofae').options[0].text = 'Artists';
        }
        </script>
        <?php
        // without if statement
        echo '<script type="text/javascript">  $(function(){  selectFunction();  }); </script>';
        ?>
    </body>
</html>

测试php回送脚本
o1
氧气
臭氧
o4
o5
o1
氧气
臭氧
o4
o5
var selectFunction=function(){
document.getElementById('ofav')。选项[0]。文本='Artists';
document.getElementById('ofae')。选项[0]。文本='Artists';
}
很好

检查:

  • 如果没有输入错误
  • 如果PHP if语句正确
  • 如果有两个

    • 像这样修改您的函数,它就会工作

      var selectFunction = function() {
          if(document.getElementById('ofae') != null){
               document.getElementById('ofae').options[0].text = 'Artists';
          }
          else if(document.getElementById('ofav') != null){
               document.getElementById('ofav').options[0].text = 'Artists';
          }
      }; 
      

      我希望这能解决您的问题

      阅读错误控制台。有可能是出了差错。如果函数被调用,它将运行其中的所有代码。。除非异常终止,如异常终止。因此,可能的情况是:1)它确实进行了更改,但随后被撤销或未被观察到;或者2)它无法正确运行且未进行更改(例如,没有适当的“#ofae”元素)。我敢打赌它是#2。。还有一个案例是“为什么这个代码不起作用?”天哪。如果要投票表决这样的问题,请至少要求一个正确的..它不起作用,只影响第一个getElementById。我修改了代码,可能是因为这两个ID在不同的页面上,而不是在同一个页面上。但是仍然不知道如何让它工作。如果你有两个不同的页面,你必须输入两次代码。你能用相关代码对你的页面进行翻页吗!?我不知道你的意思,我已经用新的信息更新了这个问题。在这里,您可以创建文件,用页面和相关代码再现完整的场景。保存它并将其作为评论发送到此处,以了解您的确切意思。
      var selectFunction = function() {
          if(document.getElementById('ofae') != null){
               document.getElementById('ofae').options[0].text = 'Artists';
          }
          else if(document.getElementById('ofav') != null){
               document.getElementById('ofav').options[0].text = 'Artists';
          }
      };