为什么我的Javascript不能在jsp页面中工作

Why is my Javascript not work in jsp page?

本文关键字:工作 jsp 我的 Javascript 不能 为什么      更新时间:2023-09-26

我创建了一个函数来检查第一个单选按钮,但我的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>

这是我的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>

好吧,这个问题的框架不正确。我在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
   }