开发者

JavaScript variables are undefined

开发者 https://www.devze.com 2023-03-07 13:22 出处:网络
I\'m writing an HTML5 app with jQTouch for mobile browsers. I\'m setting variables using code like this:

I'm writing an HTML5 app with jQTouch for mobile browsers.

I'm setting variables using code like this:

var activeproduct; // globally in js file

Then I have a ul li that sets the varialbe like so:

<li class="arrow" onclick="activeproduct=1;">
  <a href="#productClickThru">Product</a>
</li>

This works fine on FF but 开发者_JAVA技巧when I try it on iPhone's Safari I'm getting an undefined error when I try to use activeproduct inside a function.

Am I not setting up the var properly? Any help most appreciated.

Billy

As requested here is some more code (please note all list items will be generated dynamically eventually):

My javascript file:

var jQT = new $.jQTouch({
statusBar: 'black'
});

var activeproduct;
var activeroom;
var activearea;
var last_hash;

$(function(){

$(window).bind( 'hashchange',
    function(){
        console.log('unloading ' + last_hash);
        $(window).trigger('unload' + last_hash);

        last_hash = location.hash;

        console.log('loading ' + location.hash);
        $(window).trigger('load' + location.hash);
    });

$(window).bind('unload#productsMenu', function() {
    $('#productsMenuContent > *').remove();
});
$(window).bind('load#productsMenu',
    function() {
        console.log('Products menu loaded');

        $('<li class="arrow" onclick="activeproduct=1;"><a href="#productClickThru">Strip</a></li>').appendTo($('#productsMenuContent'));
        $('<li class="arrow "onclick="activeproduct=2;"><a href="#productClickThru">Prep</a></li>').appendTo($('#productsMenuContent'));
        $('<li class="arrow" onclick="activeproduct=3;"><a href="#productClickThru">Heavy Prep</a></li>').appendTo($('#productsMenuContent'));
        $('<li class="arrow" onclick="activeproduct=4;"><a href="#productClickThru">Line</a></li>').appendTo($('#productsMenuContent'));
        $('<li class="arrow" onclick="activeproduct=5;"><a href="#productClickThru">Finished Paper</a></li>').appendTo($('#productsMenuContent'));
        $('<li class="arrow" onclick="activeproduct=6;"><a href="#productClickThru">Emulsion</a></li>').appendTo($('#productsMenuContent'));
        $('<li class="arrow" onclick="activeproduct=7;"><a href="#productClickThru">Satin</a></li>').appendTo($('#productsMenuContent'));
    });

$(window).bind('load#productClickThru',
    function() {
        alert(activeproduct);
        console.log('Room: '+activeroom);
        console.log('Area: '+activearea);
        console.log('Product: '+activeproduct);
        if( activeproduct == 1 ) {
            $('#productClickThru > .toolbar > :header').html('Strip');
            $('#productClickThru').find('.room-label').html('Room: '+activeroom);
            $('#productClickThru').find('.area-label').html('Area: '+activearea);
        } else if( activeproduct == 2 ) {
            $('#productClickThru > .toolbar > :header').html('Prep');
            $('#productClickThru').find('.room-label').html('Room: '+activeroom);
            $('#productClickThru').find('.area-label').html('Area: '+activearea);
        } else if( activeproduct == 3 ) {
            $('#productClickThru > .toolbar > :header').html('Heavy Prep');
            $('#productClickThru').find('.room-label').html('Room: '+activeroom);
            $('#productClickThru').find('.area-label').html('Area: '+activearea);
        } else if( activeproduct == 4 ) {
            $('#productClickThru > .toolbar > :header').html('Line');
            $('#productClickThru').find('.room-label').html('Room: '+activeroom);
            $('#productClickThru').find('.area-label').html('Area: '+activearea);
        } else if( activeproduct == 5 ) {
            $('#productClickThru > .toolbar > :header').html('Finished Paper');
            $('#productClickThru').find('.room-label').html('Room: '+activeroom);
            $('#productClickThru').find('.area-label').html('Area: '+activearea);
        } else if( activeproduct == 6 ) {
            $('#productClickThru > .toolbar > :header').html('Emulsion');
            $('#productClickThru').find('.room-label').html('Room: '+activeroom);
            $('#productClickThru').find('.area-label').html('Area: '+activearea);
        } else if( activeproduct == 7 ) {
            $('#productClickThru > .toolbar > :header').html('Satin');
            $('#productClickThru').find('.room-label').html('Room: '+activeroom);
            $('#productClickThru').find('.area-label').html('Area: '+activearea);
        }
    });


});

index.php file:

<div id="productClickThru" class="page">
        <div class="toolbar"><h1 name="title"></h1>
            <a class="back button" href="#">Back</a>
        </div>
        <ul>
            <li><span class="room-label"></span></li> //gets set by JS
            <li><span class="area-label"></span></li> //gets set by JS
            <li>
                <select id="quantity">
                    <optgroup label="quantity">
                        <option value ="10">10</option>
                        <option value ="20">20</option>
                        <option value ="20">30</option>
                        <option value ="20">40</option>
                        <option value ="20">50</option>
                        <option value ="20">60</option>
                        <option value ="20">70</option>
                        <option value ="20">80</option>
                        <option value ="20">90</option>
                        <option value ="20">100</option>
                        <option value ="20">150</option>
                    </optgroup>

                </select>
            </li>
            <li><textarea placeholder="Notes" ></textarea></li>
        </ul>
        <a href="#" class="submit whiteButton">Save</a>
        <!-- #new-quote close -->
    </div>


You may well need to wrap your js in a self executing function such as:

<li class="arrow" onclick="javascript:(function(){activeproduct=1;})()">
    <a href="#productClickThru">Product</a>
</li>


Assign your event handlers in js file

element.onclick = function(){
   activeproduct=1;
};

It would be a better style (unobtrusive javascript) as well, because the JS is separated from HTML.

0

精彩评论

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

关注公众号