2012-03-06 225 views
1

仍然有問題 - 如果我的代碼播放選定的音頻,按鈕將不會切換。如果我包含$(document).ready(function(){,那麼按鈕切換但音頻不會播放!任何人都可以拯救我留下的小發型嗎?這是交付在iOS的HTML5 jQuery Mobile的項目5Javascript音頻播放/停止

感謝任何/所有幫助 Ĵ

代碼:

$(document).ready(function(){ 
var selSound = '18000'; //default to first sound. 
var play = false; //default to paused. 

function selectSound(id) { 
    selSound = id; 
} 

$('#playbtn').click(function() { 
    var $this = $(this); 
var a = document.getElementById(selSound); 
    if($this.text() == 'Zap!') { 
    $this.children().children().text('Stop'); 
    a.play(); 
} else { 
    $this.children().children().text('Zap!'); 
    a.pause(); 
    alert('Stopped playing song: '+selectedMP3); 



} 

}); 

}) 

HTML:

<div data-role="fieldcontain">  
     <fieldset data-role="controlgroup" > 
        <li data-swatch="b" class="ui-li ui-li-divider ui-btn ui-bar-a ui-corner-top" data-role="list-divider" role="" data-form="ui-bar-b">Select a frequency</li> 

    <input type="radio" name="mp3_option" id="selectSound('16000')" onclick="selectSound('16000')"><label for="selectSound('16000')">16Hz</label> 

    <input type="radio" name="mp3_option" id="selectSound('18000')" onclick="selectSound('18000')"><label for="selectSound('18000')">18Hz</label> 

    <input type="radio" name="mp3_option" id="selectSound('20000')" onclick="selectSound('20000')"><label for="selectSound('20000')">20Hz</label> 
</fieldset> 
<div style="text-align:center"><a href="#" data-role="button" data-inline="true" id="playbtn">Zap!</a></div></div> 
+0

你應該將'功能selectSound(ID)''的$(文件)之外。就緒(function(){});' – PeeHaa 2012-03-06 16:52:39

+0

我會開始通過改變id屬性的值來解決你的問題。通過你的javascript的外觀,你正在選擇一個無效的id(selSound ='18000')。另見http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html – 2012-03-06 17:04:17

回答

1

好吧,有點這是一個假設,但我認爲這是一個範圍問題。需要聲明的文件準備處理以外的變量和函數,但附加在它裏面的按鈕事件...

var selSound = '18000'; //default to first sound. 
var play = false; //default to paused. 

function selectSound(id) { 
    selSound = id; 
} 

$(document).ready(function(){ 
    $('#playbtn').click(function() { 
     var $this = $(this); 
     var a = document.getElementById(selSound); 
     if ($this.text() == 'Zap!') { 
      $this.children().children().text('Stop'); 
      a.play(); 
     } else { 
      $this.children().children().text('Zap!'); 
      a.pause(); 
      alert('Stopped playing song: ' + selectedMP3); 
     } 
    }); 
}) 
+0

這不是一個範圍問題。 ** selSound **和** play **都在** selectSound **和click事件處理程序之外。我實際上會保留JavaScript,因爲它在問題中。 – 2012-03-06 17:06:03

+0

我在談論的範圍是文件準備事件。在這個例子中,兩個變量都在那裏聲明,所以它們不存在於全局中。這就是我的代碼修復。 – Archer 2012-03-06 17:08:22

+0

非常感謝你的時間 - 這很完美。 - 吉姆 – Jimbly2 2012-03-06 17:08:57