【问题标题】:jQuery script not working in google chromejQuery 脚本在谷歌浏览器中不起作用
【发布时间】:2012-09-06 01:12:38
【问题描述】:

虽然看起来很奇怪,但我的脚本的一部分看起来像这样...... php 正在控制对我的 jQuery 函数的访问;请原谅这里可能有一些冗余代码,但请允许我在这里解释主要问题。

这里是一段php的代码摘录:

<?php
        if($user_info['ACTIVE'] == 'Y')
            {
                require("jQuery/main_function.js");
                require("edit_profile.php");
            }elseif(!isset($_GET['resend']))
            {
                echo '<div id="profile_info"><p>In order to modify or setup your seller profile your account must be activated.<br />
                Check your email; if you did not receive an email, <a href="profile.php?resend">Click here</a> to have another one dispatched.</p>';
                echo '<p>You can however complete your contact information <a href="user_contact.php">here</a>';
            }

正如您在 php 代码中看到的,如果用户是活动的,根据数据库调用确定,它将需要 html 中的 main_function.js,因此删除:

<script type="text/javascript">
function option_click(elementId)
    {
        $(document).ready(function(){
            alert('testing1');
            var defaultString = "Upload";
            var elementNum = elementId.substr(elementId.length-1, 1);
            switch($('#'+elementId).val())
            {
                case 'Video':
                {
                    alert('testing in chrome');
                    $('.set_time'+elementNum).prop('type', 'hidden');
                    $('.product_upload'+elementNum).prop('type', 'file');
                    $('.content_upload'+elementNum).html(defaultString + ' a ' + 'video file:');
                    break;  
                }
                case 'Time':
                {
                    $('.content_upload'+elementNum).html('How much time?');
                    $('.product_upload'+elementNum).prop('type', 'hidden');
                    $('.set_time'+elementNum).prop('type', 'text');
                    break;
                }
                case 'Music/Audio':
                {
                    $('.set_time'+elementNum).prop('type', 'hidden');
                    $('.product_upload'+elementNum).prop('type', 'file');
                    $('.content_upload'+elementNum).html(defaultString + ' a ' + 'music file');
                    break;
                }
                case 'Ebook':
                {
                    $('.set_time'+elementNum).prop('type', 'hidden');
                    $('.product_upload'+elementNum).prop('type', 'file');
                    $('.content_upload'+elementNum).html(defaultString + ' an ' + 'ebook file');
                    break;  
                }
                case 'Tutorial/Docs':
                {
                    $('.set_time'+elementNum).prop('type', 'hidden');
                    $('.product_upload'+elementNum).prop('type', 'file');
                    $('.content_upload'+elementNum).html(defaultString + ' a ' + 'tutorial file');  
                    break;
                }
            }
        });
    }
</script>

在我的代码中间...

在除谷歌浏览器之外的所有浏览器中都能正常工作吗?为什么?

【问题讨论】:

  • 控制台中是否有任何异常可以与我们分享?
  • 你不能使用 require("jQuery/main_function.js");使用 include 而不是读取内容并在 php 加载之前回显它。
  • @JapanPro 为什么include 会比require 更好?
  • 为什么你的 switch 语句中有那些花括号?
  • 酷——至少你知道你的脚本被调用了!它可能与 Sheikh 发布的答案有关(关于 val())?

标签: php javascript jquery google-chrome


【解决方案1】:

您正在切换选项的价值,例如

switch($('#'+elementId).val())

但在您的元素(选项)中,我没有看到任何 value 分配(来自您提供的评论)

<option id="video0" onclick="option_click(this.id)">Video</option>

更新:

您的方法(不适用于 Chrome)

<select>
   <optgroup label="Swedish Cars">
       <option id="volvo" onclick="option_click(this.id)">Volvo</option>
       <option id="saab" onclick="option_click(this.id)">Saab</option>
   </optgroup>

DEMO.

正确的方式(在所有浏览器中工作)

<select onchange="option_click(this.value)">
    <optgroup label="Swedish Cars">
        <option value="volvo" id="volvo">Volvo</option>
        <option value="saab" id="saab">Saab</option>
    </optgroup>
</select>

DEMO.

An example with switch statement here.

【讨论】:

  • 值变为 Video... 通常值是选项标签之间的值。
  • 如前所述,这适用于firefox,即opera等,而不是chrome
  • 非常感谢...感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-15
  • 2020-04-11
  • 1970-01-01
  • 2012-10-12
  • 2016-01-25
相关资源
最近更新 更多