2015-11-15 173 views
0

我創建的功能,使第一個放射按鈕被選中,但我的JavaScript不工作。當我在jsfiddle中使用它時它工作完美,但是當我嘗試在jsp頁面中時,它不再工作。誰能告訴我我做錯了什麼?我使用struts 1,並且不使用任何jquery庫。爲什麼我的Javascript不能在jsp頁面中工作?

這是我的代碼:

document.querySelectorAll('[name=radioButton]')[0].checked = true;
\t <table id="tabledata" style="width: 80%" border=1> 
 
\t \t \t <tr> 
 
\t \t \t \t <th></th> 
 
\t \t \t \t <th>Name</th> 
 
\t \t \t \t <th>Code</th> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t \t <td><input type="radio" name="radioButton"></td> 
 
\t \t \t \t <td>122222222222222</td> 
 
\t \t \t \t <td>4444444444444444 </td> 
 
\t \t \t </tr> 
 
      <tr> 
 
\t \t \t \t <td><input type="radio" name="radioButton"></td> 
 
\t \t \t \t <td>122222222222222</td> 
 
\t \t \t \t <td>4444444444444444 </td> 
 
\t \t \t </tr> 
 
      <tr> 
 
\t \t \t \t <td><input type="radio" name="radioButton"></td> 
 
\t \t \t \t <td>122222222222222</td> 
 
\t \t \t \t <td>4444444444444444 </td> 
 
\t \t \t </tr> 
 
      <tr> 
 
\t \t \t \t <td><input type="radio" name="radioButton"></td> 
 
\t \t \t \t <td>122222222222222</td> 
 
\t \t \t \t <td>4444444444444444 </td> 
 
\t \t \t </tr> 
 
\t \t \t </table>

這是我的JSP代碼:

<html:form action="/Showdata"> 
    <div class="loaddata"> 
    <table id="tabledata" style="width: 80%" border=1> 
      <tr> 
       <th></th> 
       <th>Name</th> 
       <th>Code</th> 
      </tr> 
      <logic:iterate id="listEmp" name="ShowdataForm" property="listEmp"> 
      <tr> 
       <td><input type="radio" name="radioButton"></td> 
       <td><bean:write name="listEmp" property="name"/></td> 
       <td><bean:write name="listEmp" property="code"/> </td> 
      </tr> 
      </logic:iterate> 
      </table> 
    </div> 
</html:form> 
+1

如果你不使用jQuery爲什麼你在你的代碼一堆的jQuery? –

+2

'我的javascript無法正常工作' - 你必須做得比這更好......它怎麼不起作用?提示:大多數瀏覽器都具有**開發人員工具**,其中您將擁有**控制檯** - 此控制檯*將顯示消息和JavaScript錯誤,有用的信息爲JavaScript ** _開發人員_ **如自己。你的** _ code _ **在*控制檯*中產生了什麼? –

+0

你不需要重複'$(function(){'。它相當於'$(document).ready(function(){'。 – Andy

回答

0

好,這個問題沒有正確陷害。我在JavaScript代碼中發現了一些錯誤。

$(document).ready(function() { 
//$(function() {remove this 
    alert("Hello! I am an alert box!!"); 
$("input:radio[name='radioButton']:visible:first").attr('checked', 'checked'); 
    }//why do you have a curly brace here? 
); 
$(function() { 
    $('#tabledata tr').click(function() { 
     $(this).find('tr input:radio').prop('checked', true); 
    }) 
//}); 
}); 

如果你試着當一個單選按鈕被選中嘗試做一些這

$('input:radio[name="radioButton"]').change(
function(){ 
    if (this.checked && this.value == 'Yes') { 
     // called on checking an <input>, not 
     // on un-checking it. 
    } 
}); 

    $(document).ready(function() {//put a block of code in here to run it as soon as document loads. 
    alert("page loaded"); 
    //no need of putting a $(function(){ }); here again 
    } 
+0

哦謝謝你的幫助,但我發佈錯誤的代碼,你可以查看我的問題,並再次幫助我嗎? – thefriend

相關問題