在经典 ASP 和 jQuery 中使用复选框值填充文本框

Populate Textbox with Checkbox Value in classic ASP and jQuery

本文关键字:复选框 填充 文本 ASP 经典 jQuery      更新时间:2023-09-26

这是我的代码片段:

 response.write "<th align=left><font size=2>"
    response.write "1. <input type=checkbox class='checkboxes' value='ORG'>Organization"
    response.write "</font> </th>"
    response.write "<th align=left><font size=2>"
    response.write "2. <input type=checkbox  value='OpType' class='checkboxes'>Operation Type"
    response.write "</font> </th>"
    response.write "<th align=left><font size=2>"
    response.write "3. <input type=checkbox checked  value='YEAR' class='checkboxes'>Year"
    response.write "</font> </th>"
response.write "<tr><td colspan='3'> <input name='DISTRIBUTION' size='45' /></td></tr>"

这是JavaScript。

<script type="text/javascript" src="../Scripts/jquery-1.9.1.js"></script>
$('.checkboxes').change(function () {
    alert("1");
    $("input[Title='DISTRIBUTION']").val("");
    if ($('.checkboxes').is(':checked')) {
        $("input[Title='DISTRIBUTION']").val("Yes");
    }
});

我没有看到警报,所以它看起来不像被触发了。 我做错了什么吗?

也许脚本是在文档完全构建之前执行的?只是一个猜测,但尝试将你的 jquery 代码包装在其中;

$(document).ready(function () { ... });

你还应该看看 KnockoutJs - 它会让你的生活更轻松......

我怀疑这只是一种不等到 DOM 准备就绪后再分配事件处理程序的情况。 试试这个...

$(function() {
    $('.checkboxes').change(function () {
        alert("1");
        $("input[Title='DISTRIBUTION']").val("");
        if ($('.checkboxes').is(':checked')) {
            $("input[Title='DISTRIBUTION']").val("Yes");
        }
    });
});