简体   繁体   English

Javascript 在 Firefox 中有效,但在 Chrome 中无效

[英]Javascript works in Firefox but not in Chrome

My JavaScript code works fine in Firefox, but not in chrome.我的 JavaScript 代码在 Firefox 中运行良好,但在 Chrome 中却不行。 While in Firefox the correct number of forms (built by the code within the JavaScript function) are shown as soon as the option is selected, in Chrome nothing happens.在 Firefox 中,一旦选择该选项,就会显示正确数量的表单(由 JavaScript 函数中的代码构建),而在 Chrome 中什么也没有发生。 I'm not sure why.我不确定为什么。 I haven't checked in a while since I mostly use a Linux computer, but I believe IE9 works as well.我有一段时间没有检查了,因为我主要使用 Linux 计算机,但我相信 IE9 也能正常工作。

If you could help me find I solution I'd appreciate it.如果您能帮助我找到解决方案,我将不胜感激。 By the way, I did read the other similarly titled question, but it didn't seem to answer this.顺便说一句,我确实阅读了另一个类似标题的问题,但它似乎没有回答这个问题。

<script type="text/javascript">
  function showForms(numForms)
  {
      // build  the form
      var form = "";
      for(i = 1; i <= numForms; i++)
      {    
           // define one section of the form (give fields unique names).
           var formPart = '<fieldset><legend>Add User</legend><label>Full Name:</label><input name="fullname' + i + '"  type="text" required="required"" /><label>Email:</label><input name="email' + i + '" type="email" /><label>Phone:</label><input placeholder="1 (123)-123-1234" name="phone' + i + '" type="tel" /><label>Spreadsheet:</label><input name="spreadsheet' + i + '" type="file" /><label>Registration #:</label><textarea name="regnums' + i + '" placeholder="Aircraft registration number(s). Separate each number with a comma."></textarea></fieldset>';
           form = form + formPart;
      }
      // output the form to the page
      var output = document.getElementById("output");
      output.innerHTML = form + '<input name="numForms" value="' + numForms + '" type="hidden" />';
  }
</script>

By the way, this function is called by <option onclick="showForms(#)">#</option> where # is the number of forms to be displayed.顺便说一句,此函数由<option onclick="showForms(#)">#</option>调用,其中 # 是要显示的表单数。 The options, of course, reside within a <select> .当然,选项位于<select>中。

Thank you in advance for your help.预先感谢您的帮助。

EDIT: Here is the calling script, containing HTML, PHP & JavaScript.编辑:这是调用脚本,包含 HTML、PHP 和 JavaScript。

<?php
// include the javascript function to generate the form
include ('../private/Includes/Scripts/showforms.js');

// form handling code
if (isset($_POST['submit']))
{
    // make sure the input for the number of users is a number, and less than or
    // equal to 10
    if (is_numeric($_POST['numForms']) && $_POST['numForms'] <= 10)
    {
        $regCount = $_POST['numForms'];
    }

    // initialize array for form data
    $regData = array();
    // fill array with multi-dimentional form data
    for ($i = 1; $i <= $regCount; $i++)
    {
        // get form data and store in temporary variables
        $fullname = trim($_POST['fullname' . $i]);
        $email = trim($_POST['email' . $i]);
        $phone = trim($_POST['phone' . $i]);
        $spreadsheet = $_POST['spreadsheet' . $i];
        $regnums = trim($_POST['regnums' . $i]);
        // put data in array
        $regData[$i] = array(
            'username' => '',
            'fullname' => $fullname,
            'email' => $email,
            'phone' => $phone,
            'spreadsheet' => $spreadsheet,
            'regnums' => $regnums
        );
        // unset temporary variables
        unset($invalid, $username, $fullname, $email, $phone, $spreadsheet, $regnums);
    }
    $errors = registerUsers($regData);
    if ($errors[0] == "Some users were not registered due to missing usernames.")
    {
        $notes[] = "Some users were not registered due to missing usernames.";
        unset($errors[0]);
    }
    if (!isset($errors))
    {
        $success = true;
    }
}
?>
<h2 style="margin-top: -9px;">Register Users</h2>
<p>Use this form to register new users. Usernames and passwords are automatically generated and emailed to their owner's addresses.</p>
<form enctype="multipart/form-data" method="post">

    <fieldset>
        <legend>
            Form Setup &amp; Results
        </legend>
        <label>No. of User(s):</label>
        <select onchange="showForms(0)" name="select">
            <option selected="selected">- Please Select -</option>
            <option onclick="showForms(1)">1</option>
            <option onclick="showForms(2)">2</option>
            <option onclick="showForms(3)">3</option>
            <option onclick="showForms(4)">4</option>
            <option onclick="showForms(5)">5</option>
            <option onclick="showForms(6)">6</option>
            <option onclick="showForms(7)">7</option>
            <option onclick="showForms(8)">8</option>
            <option onclick="showForms(9)">9</option>
            <option onclick="showForms(10)">10</option>
        </select>
    </fieldset>
    <div id="output"></div>
    <input id="submit" name="submit" type="submit" value="Submit" />
</form>

Wrong syntax - unescaped " in the HTML string.语法错误 - 未转义 " 在 HTML 字符串中。

... + '"  type="text" required="required"" />

Use some IDE or smart editor, that would save you from such mistakes.使用一些 IDE 或智能编辑器,可以避免此类错误。

BTW one trick for you: Put the code template into a hidden (display: none);顺便说一句,给你一个技巧:将代码模板放入隐藏(显示:无); Then, for each, copy it to a new node and change it's IDs, and put it to the right place.然后,对于每一个,将它复制到一个新节点并更改它的 ID,然后将它放在正确的位置。 See DOM manipulation, part of W3C standards.请参阅 DOM 操作,W3C 标准的一部分。

Second tip: Use your server side lang support for arrays like PHP's foo[] instead of generating IDs.第二个提示:使用服务器端对数组的 lang 支持,例如 PHP 的 foo[] 而不是生成 ID。 IMO better approach.海事组织更好的方法。

I'm guessing you have more than one element with the ID "output" because I just appended such an element to this page on stack in Chrome, executed your func definition, fired it with an argument of 8 and got a bunch of forms.我猜你有不止一个 ID 为“output”的元素,因为我只是将这样一个元素附加到 Chrome 堆栈上的这个页面,执行你的 func 定义,用参数 8 触发它并得到一堆表格。 Stack uses JQuery so... Stack 使用 JQuery 所以...

$('body').append('<div id="output"/>');

Then cut and paste to define your func in the console and...然后剪切并粘贴以在控制台中定义您的功能,然后...

showForms(8);

You should see a bunch of form junk at the bottom of the page in Stack.您应该在 Stack 的页面底部看到一堆表单垃圾。 Another possibility is that you have no elements with the ID 'output'另一种可能性是您没有 ID 为“output”的元素

Always check the HTML first.始终首先检查 HTML。 Always.总是。 It takes very little time.只需要很少的时间。 If you have JQ on your site, just do this before you call that function.如果您的站点上有 JQ,只需在调用该函数之前执行此操作。

alert($('#output').size());

If it's not 1, that's your problem.如果它不是 1,那是你的问题。

I had a similar problem that ended up being due to not defining my arrays prior to use.我有一个类似的问题,最终是由于在使用前没有定义我的数组。 Firefox seems to be more tolerant of working with arrays that have not been explicitly defined with a var statement, but Chrome needs them to be defined. Firefox 似乎更能容忍未使用 var 语句明确定义的数组,但 Chrome 需要定义它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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