2010-01-06 102 views
1

Bellow是我的php代碼。php上的javascript問題

<div class="poller"> 

       <div class="poller_question" id="poller_question2"> 
       <p class="pollerTitle">Would you join a Community Action Committee?</p> 
<p class="pollerOption"><input type="radio" value="6" name="vote[2]" id="pollerOption6"> 
<label for="pollerOption6" id="optionLabel6">Yes</label></p><p class="pollerOption"> 
<input type="radio" value="7" name="vote[2]" id="pollerOption7"><label for="pollerOption7" 
id="optionLabel7">Maybe</label></p><p class="pollerOption"><input type="radio" value="8" 
name="vote[2]" id="pollerOption8"><label for="pollerOption8" id="optionLabel8">No</label></p>   
       <a href="#" onclick="castMyVote(2,document.forms[0])"><imgsrc="images/vote_button.gif" border="0"></a> 

       </div> 
       <div class="poller_waitMessage" id="poller_waitMessage2"> 
        Getting poll results. Please wait... 
       </div> 
       <div class="poller_results" id="poller_results2"> 
       <!-- This div will be filled from Ajax, so leave it empty --></div> 
      </div> 

貝婁是我的javascript代碼

function castMyVote(pollId,formObj) 
{ 
    var elements = formObj.elements['vote[' + pollId + ']']; 
    var optionId = false; 
    for(var no=0;no<elements.length;no++){ 
     if(elements[no].checked)optionId = elements[no].value; 
    } 
    Poller_Set_Cookie('dhtmlgoodies_poller_' + pollId,'1',6000000); 
    if(optionId){ 

     var ajaxIndex = ajaxObjects.length; 
     ajaxObjects[ajaxIndex] = new sack(); 
     ajaxObjects[ajaxIndex].requestFile = serverSideFile + '?pollId=' + pollId + '&optionId=' + optionId; 
     prepareForPollResults(pollId); 
     ajaxObjects[ajaxIndex].onCompletion = function(){ showVoteResults(pollId,ajaxIndex); }; // Specify function that will be executed after file has been found 
     ajaxObjects[ajaxIndex].runAJAX();  // Execute AJAX function  

    } 
} 

形式的PHP我調用一個名爲 「castMyVote」 .The JavaScript方法JavaScript是

var elements = formObj.elements['vote[' + pollId + ']']; 

創建問題?爲什麼?如何解決呢?VAR元素沒有價值

+1

你能解釋一下你的問題到底在哪? – junmats 2010-01-06 05:46:38

+0

你應該投資一些格式。除此之外,它使調試更容易。 – 2010-01-06 09:29:32

+0

你得到的錯誤是什麼? – 2010-01-06 05:36:48

回答

0

嘗試

var elements = document.getElementsByName("vote["+pollId+"]"); 
0

如果你的問題是,你的元素變量沒有得到正確的值,那是因爲你的函數的第二個參數CastMyVote需要一個表單,但是您的html中沒有表單。 試試這個:

<div class="poller"> 
<form method="post" action="#"> 
       <div class="poller_question" id="poller_question2"> 
       <p class="pollerTitle">Would you join a Community Action Committee?</p> 
<p class="pollerOption"><input type="radio" value="6" name="vote[2]" id="pollerOption6"> 
<label for="pollerOption6" id="optionLabel6">Yes</label></p><p class="pollerOption"> 
<input type="radio" value="7" name="vote[2]" id="pollerOption7"><label for="pollerOption7" 
id="optionLabel7">Maybe</label></p><p class="pollerOption"><input type="radio" value="8" 
name="vote[2]" id="pollerOption8"><label for="pollerOption8" id="optionLabel8">No</label></p>   
       <a href="#" onclick="castMyVote(2,document.forms[0])"><imgsrc="images/vote_button.gif" border="0"></a> 

       </div> 
       <div class="poller_waitMessage" id="poller_waitMessage2"> 
        Getting poll results. Please wait... 
       </div> 
       <div class="poller_results" id="poller_results2"> 
       <!-- This div will be filled from Ajax, so leave it empty --></div> 
</form> 
      </div> 

我只是把你的div和輸入的表單內。

編輯:您的<imgsrc="images/vote_button.gif" border="0">。錯字錯誤?