2015-11-15 98 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

相关问题