开发者

Calculating two element onkeyup - How can I convert this to jQuery?

开发者 https://www.devze.com 2023-01-24 06:38 出处:网络
Im having a trouble converting my scripts to jQuery. How can I compute two elements at once? <script type=\"text/javascript\">

Im having a trouble converting my scripts to jQuery. How can I compute two elements at once?

<script type="text/javascript">
function Compute(target, currentValue){
if(parseInt(currentValue.value) == 0 || parseInt(currentValue.value) > parseInt(target.value))
{
   alert("That Value is not Valid");
}       
}
</script>

<input type="text" id="frm1" name="frm1" onkeyup="Compute(document.getElementById('frm1a'), this);"/> <br>
<input type="hidden" id="frm1a" name="frm1a" value="开发者_如何学Python90"/> <br>

<input type="text" id="frm2" name="frm2" onkeyup="Compute(document.getElementById('frm2a'), this);"/> <br>
<input type="hidden" id="frm2a" name="frm2a" value="50"/> <br>

<input type="text" id="frm3" name="frm3" onkeyup="Compute(document.getElementById('frm3a'), this);"/> <br>
<input type="hidden" id="frm3a" name="frm3a" value="10"/> <br>


Something like

$(function(){
    $("input:text[name*='frm']").bind("keyup", function(){
        var currentValue = $(this);
        var target = $("#" + currentValue.attr("id") + "a");
        var currentValueVal = currentValue.val();

        if (parseInt(currentValueVal) === 0 || parseInt(currentValueVal) > parseInt(target.val()))
        {
             alert("That Value is not Valid");
        }
    });
});

See a working demo


I can't see much of an advantage in converting this to jQuery... there is not much javascript and the markup does not benifit a huge amount from adding jQuery features and your users will have to download the jQuery library when using your site (small it is, but still...)

I could only suggest removing the document.getElement and replacing that with jQuery.

<input type="text" id="frm1" name="frm1" onkeyup="Compute($('#frm1a'), this);"/> <br>
<input type="hidden" id="frm1a" name="frm1a" value="90"/> <br>

Though I am open to being corrected

0

精彩评论

暂无评论...
验证码 换一张
取 消