jQuery |使用jQuery在文本上显示值已更改

时间:2022-12-18 20:26:36

I have made a jQuery function to change the text of a readonly (not sure if this could be why, didn't work on not readonly) text input field, my code is this:

我已经制作了一个jQuery函数来改变readonly的文本(不确定这可能是为什么,不能在readonly上工作)文本输入字段,我的代码是这样的:

$(document).ready(function () {
    $("#stake").textchanged(function () {
        $('#dynamic_return').style.display = '';
        $("#showdynamicreturn").value = $("#stake") * $("#__odds");
    });
});

Is the event correct or is something else wrong as this doesn't work? (I am using VS 2015 preview and it doesn't say it's wrong but it doesn't say it exists either...)

事件是否正确或是否有其他错误,因为这不起作用? (我正在使用VS 2015预览,它并没有说它是错的,但它并没有说它存在......)

Edit
HTML code:

编辑HTML代码:

<form action="/sports/football/" method="post">
    <table>
        <tbody>
            <tr>
                <td style="width: 50%;">Sport: </td>
                <td>Horse Racing</td>
            </tr>
            <tr>
                <td style="width: 50%">Participant: </td>
                <td>Whale Trail</td>
            </tr>
            <tr>
                <td>Market: </td>
                <td>Scottsville</td>
            </tr>
            <tr>
                <td>Time: </td>
                <td>15:40</td>
            </tr>
            <tr>
                <td>Odds: </td>
                <td>3/1<input type="hidden" value="" id="__odds"></td>
            </tr>
            <tr>
                <td>Stake: </td>
                <td>
                    <div class="input-group"><span class="input-group-addon" id="basic-addon1">£</span><input style="width: 100%" type="text" name="stake" aria-describedby="basic-addon1" placeholder="Stake" onblur="return estimate()"></div>
                </td>
            </tr>
            <tr style="display: none;" id="dynamic_return">
                <td colspan="10">
                    <center><input type="text" id="showdynamicreturn" placeholder="Estimated return" readonly=""></center>
                </td>
            </tr>
            <tr>
                <td>
                    <center><label for="eachWay">Each way bet?&nbsp;&nbsp;</label><input type="checkbox" id="eachWay" name="eachWay"></center>
                </td>
                <td>
                    <center><input type="submit" name="submitTo__OpenBets" value="Open Bet"></center>
                </td>
            </tr>
            <!-- Hidden fields for the horses information. -->
            <input type="hidden" name="betslip_id" value="34"><input type="hidden" name="sport" value="football"><input type="hidden" name="odds" value="3/1"><input type="hidden" name="sport" value="Horse Racing"><input type="hidden" name="bettilldate" value="2015-05-13"><input type="hidden" name="bettilltime" value="15:40:00"><!-- Area to "submit a delete" and remove an item from the bet slip. --><tr>
                <td colspan="100%">
                    <center><input type="submit" name="delete_betslip_item" value="Delete this bet"></center>
                </td>
            </tr>
        </tbody>
    </table>
</form>

2 个解决方案

#1


First, in your html, you do NOT have any element with id "stake".

首先,在你的html中,你没有任何id为“stake”的元素。

Second, if you want to do this, use jQuery method on('change) or change.

其次,如果你想这样做,请使用jQuery方法('更改)或更改。

#2


This is how I managed to do it, with help from the community of course:

这就是我设法做到这一点的方式,当然还有社区的帮助:

$(document).ready(function () {
    $("#stake").on('change', function () {
        var newVal = (parseFloat($("#stake").val(), 10) * parseFloat($('#__odds').val(), 10)) + parseFloat($("#stake").val(), 10);
        $("#showdynamicreturn").val(parseFloat(newVal).toFixed(2));
    });
});

Also - removed the show() from the equation as this was unnecessary

另外 - 从等式中删除了show(),因为这是不必要的

#1


First, in your html, you do NOT have any element with id "stake".

首先,在你的html中,你没有任何id为“stake”的元素。

Second, if you want to do this, use jQuery method on('change) or change.

其次,如果你想这样做,请使用jQuery方法('更改)或更改。

#2


This is how I managed to do it, with help from the community of course:

这就是我设法做到这一点的方式,当然还有社区的帮助:

$(document).ready(function () {
    $("#stake").on('change', function () {
        var newVal = (parseFloat($("#stake").val(), 10) * parseFloat($('#__odds').val(), 10)) + parseFloat($("#stake").val(), 10);
        $("#showdynamicreturn").val(parseFloat(newVal).toFixed(2));
    });
});

Also - removed the show() from the equation as this was unnecessary

另外 - 从等式中删除了show(),因为这是不必要的