简体   繁体   English

jQuery如何绑定最后一位数字

[英]Jquery How to bind the last digit

i have a table like below which each content is input pop-up 我有一个像下面的表格,每个内容都输入弹出式窗口 在此处输入图片说明

on square i spot you see on last digit have 1,2,8,9 format. 在正方形上,我发现您在最后一位看到的数字具有1,2,8,9格式。

in my html the content of table is value of Posisi 在我的html中,表格的内容是Posisi的价值

Nomor Rak
     <br><input type="text" id="posisi" readonly/></br>

that automaticly i pick using 我自动选择使用

<td class="data206"><div align="center"><input class="data206" type="button" onclick="popup_window_show('#sample', { pos : 'tag-right-down', parent : this});setvalue(this.value);" value="A1.7.8" /></div></td>

for an example. 举个例子 so my pop-up like this 所以我的弹出窗口是这样的 在此处输入图片说明

my purpose to bind the last digit and then i can manipulate line value. 我的目的是绑定最后一位数字,然后我就可以操纵线值。 So for an idea script will like below 所以对于一个想法脚本将如下所示

$(document).ready(function(){
var posisi = $("#posisi").val();
if (posisi== like "1" or like "2" or like "8" or like "9" ){
$("#line").val("9")
}

}); });

My problem: I don't know how to bind the last digit in jquery..to make conditonal that $("#posisi") last digit value between 1 or 2 or 8 or 9. if this conditional true i can add value in $("#line").val("whatever i want") need help with an example will be appreciate 我的问题:我不知道如何绑定jquery ..中的最后一位数字,以使$(“#posisi”)最后一位数字值介于1或2或8或9之间。如果此条件为true,我可以在其中添加值$(“#line”)。val(“我想要的一切”)需要帮助的示例将不胜感激

var posisi = $("#posisi").val();
var regex = /\.(\d+)\.?$/;
// Catches the last set of digits following a period. 
// Allowing for an additional period at the end.

var matches = posisi.match(regex); 
if (matches) {
    var lastNumber = parseInt(matches[1], 10);
    // Do something 
    $("#line").val(lastNumber);
}

Your var posisi will contain a string like "A1.7.8" and you can easily get the last character of string. 您的var posisi将包含一个字符串,例如“ A1.7.8”,您可以轻松获取字符串的最后一个字符。 This may help How can I get last characters of a string using JavaScript 这可能会帮助我如何使用JavaScript获取字符串的最后一个字符

There are a few more tips which may help. 还有一些技巧可能会有所帮助。 It seems that you do not want to wrap the code in document.ready. 似乎您不想将代码包装在document.ready中。 I think you want to get the last digit value on every click. 我认为您希望每次点击都获得最后一位数字。 so get the value in "popup_window_show" function or function you are using to show popup. 因此,请在“ popup_window_show”函数或用于显示弹出窗口的函数中获取值。

Moreover if you want to make calculation on that number ie 1,2,8,9 then convert it into integer form first. 此外,如果要对该数字(即1,2,8,9)进行计算,请先将其转换为整数形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM