dongmangzong8006 2014-04-10 02:03
浏览 58
已采纳

JQuery忽略click()和change()

Advance note: I have already looked at and looked at and tried the solutions in the SO question: Jquery checkbox change and click event. Others on SO deal more with reading the values rather than the issue that the event is not firing.

I will admit that I am a relative newbie to JQuery.

I am trying to make two changes. One when a text field changes and one when a checkbox is toggled. Both are within a form. The idea is that if the text field changes a computation is done and the return value is written into the text after the checkbox. The checkbox toggles that text on and off. Once finished the form can be submitted.

the code (as seen below) also uses php.

I've pulled the relevant code. I read several examples on line so there is are attempts using

  • <span id="foo"><input></span>
  • <input class='romanCheck' id='romanCheck' type='checkbox'>

Neither alert is being called. JSFiddle kinda barfed on the PHP. For the checkbox I've tried both .change() and .click()

The browsers I've tested on are all Mac (my dev environ)

  • Safari: 7.0.3 (9537.75.14)
  • Chrome: 33.0.1750.152
  • Firefox: 28.0

I've attached the relevant code.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Not Working.php</title>
    <link rel="stylesheet" href="jquery-ui-1.10.4.custom/css/ui-lightness/jquery-ui-1.10.4.custom.css">
    <script src="jquery-ui-1.10.4.custom/js/jquery-1.10.2.js"></script>
    <script src="jquery-ui-1.10.4.custom/js/jquery-ui-1.10.4.custom.js"></script>
</head>
<body>
<script>
function romanize (num) {
    return "(" + "roman for " + ")";
}

$(document).ready(function(){

    $(".romanCheck").live("change",function() {
        alert("#romanCheck.click has been hit");
        var romanValue = "";
        var roman = $("#romanCheck").is(':checked');
        if ( roman ) {
            var itValue = $(this).val();
            romanValue="(" + romanize(itValue) +")";
        }
        $("#romanDisplay").text(romanValue);
    });

    $("span.iterationField input").live("change",function() {
        alert("#iteration.change has been hit");
        var romanValue = "";
        var roman = $("#romanCheck").is(':checked');
        if ( roman ) {
            var itValue = $(this).val();
            romanValue="(" + romanize(itValue) +")";
        }
        $("#romanDisplay").text(romanValue);
    });
});

</script>
<form action='EventValidateProcess.php' method='post'>
<?php
    $doesShow = 1;
    $isRoman = 1;
    $iteration - 13;
    print "<table>
";
    print "<tr>
\t<td>Iteration</td>
\t";
    print "<td><span id='iterationField'><input type='text' name='iteration' value='" . $iteration . "'></span></td>
\t";
    print "<td><input type='checkbox' name='doesShow' value='1'";
    if ($doesShow == 1) {
        print " checked";
    }
    print "> visible | ";

    print "
<input class='romanCheck' id='romanCheck' type='checkbox' name='isRoman' value='1'";
    if ($isRoman == 1) {
        print " checked";
    }
    print "> uses Roman numerals
";
    print "<span id='romanDisplay'>(XX)</span></td>
</tr>
";
?>
</table>
<button type='submit' name='process' value='update'>Update</button><br/>
</form>
</body>
  • 写回答

4条回答 默认 最新

  • dongmen5867 2014-04-10 03:27
    关注

    After a lot of finessing it seemed the answer that worked best was the following:

        $(document).change("input.romanCheck", function() {
            var romanValue = "";
            var roman = $("#romanCheck").is(':checked');
            if ( roman ) {
                var itValue = $("#iterationField").val();
                romanValue="(" + romanize(itValue) +")";
            }
            $("#romanDisplay").text(romanValue);
        });
    
        $(document).change("input.iterationField", function() {
            var romanValue = "";
            var roman = $("#romanCheck").is(':checked');
            if ( roman ) {
                var itValue = $("#iterationField").val();
                romanValue="(" + romanize(itValue) +")";
            }
            $("#romanDisplay").text(romanValue);
        });
    

    Using:

        print 
        "<input id='iterationField' type='text' name='iteration' value='";
        print $iteration . "'/>";
    
        print 
        "<input id='romanCheck' type='checkbox' name='isRoman' value='1'";
        if ($isRoman == 1) {
            print " checked";
        }
        print ">";
    
        print "<span id='romanDisplay'>";
        if ($isRoman == 1) {
            print "(" . $romanIteration . ")";
        }
        print "</span>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python