我创建函数来检查第一个radion按钮,但是我的javascript无效。当我在jsfiddle中使用它时,它工作得很完美但是当我在jsp页面中尝试时,它不再工作了。谁能告诉我我做错了什么?我使用struts 1,并且不使用任何jquery库。
这是我的代码:
document.querySelectorAll('[name=radioButton]')[0].checked = true;

<table id="tabledata" style="width: 80%" border=1>
<tr>
<th></th>
<th>Name</th>
<th>Code</th>
</tr>
<tr>
<td><input type="radio" name="radioButton"></td>
<td>122222222222222</td>
<td>4444444444444444 </td>
</tr>
<tr>
<td><input type="radio" name="radioButton"></td>
<td>122222222222222</td>
<td>4444444444444444 </td>
</tr>
<tr>
<td><input type="radio" name="radioButton"></td>
<td>122222222222222</td>
<td>4444444444444444 </td>
</tr>
<tr>
<td><input type="radio" name="radioButton"></td>
<td>122222222222222</td>
<td>4444444444444444 </td>
</tr>
</table>
&#13;
这是我的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>
答案 0 :(得分:0)
$(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
}