简体   繁体   English

JQuery脚本中的XOR值

[英]XOR value in JQuery script

I have a small script where I need one value to xor. 我有一个小脚本,需要一个值进行异或。 Someone pointed out how to do it, but I have since changed the script and his suggestion doesn't apply here anymore. 有人指出了操作方法,但此后我更改了脚本,他的建议在这里不再适用。

<input type='button' value='Ein / Aus' class='taster' csv='Lampe' />        

<script>            
    $(document).on('click', 'input.taster', function() {

        // Get ID from Input
        var id = $(this).attr('csv');

        $.get('plc.php?function=csv&id=' + id, function(data){
            var csv = data.split(',');

            //Output
            var out = csv[1];

            //Write
            var wdatatyp = csv[2];
            var wdb = csv[3];
            var wbyte = csv[4];
            var wbit = csv[5];      
            var bitval = csv[6];

            //Read
            var rdatatyp = csv[7];
            var rdb = csv[8];
            var rbyte = csv[9];
            var rbit = csv[10];

            $(document).load('plc.php?function=write-bit', {'wdatatype':wdatatyp, 'wdb':wdb, 'wbyte':wbyte, 'wbit':wbit, 'bitval':bitval}); 

            function read() {               
                $(out).load('plc.php?function=read-bit', {'rdatatype':rdatatyp, 'rdb':rdb, 'rbyte':rbyte, 'rbit':rbit});                    
            }
            setTimeout(read, 100);                  
        });             
    });         
</script>

A little cleanup and better description of what needs to happen. 进行一些清理并更好地描述需要发生的情况。

csv[6] is fetched from the csv file and will always return 1. 从csv文件中提取了csv [6],并将始终返回1。

So what I need is the first time the script is called, bitval needs to return a 1. The next time it needs to return a 0, then a 1, then a 0, etc. 所以我需要的是第一次调用该脚本,bitval需要返回1。下次需要返回0,然后是1,然后是0,依此类推。

You need to convert the bitval to an int before XORing it. 您需要bitval转换为int然后再对其进行XOR。 So replace the line 所以更换线

var bitval = csv[6];

with

var bitval = parseInt(csv[6]);

The toggling can be then done this way (by moving the bitval declaration out of the onClick handler): 然后可以通过以下方式进行切换(通过将bitval声明移出onClick处理程序):

var bitval = null; // this is crucial since you don't have another place to store the value
$(document).on('click', 'input.taster', function() {
    var id = $(this).attr('csv');
    $.get('plc.php?function=csv&id=' + id, function(data){
        // your variables
        if (bitval == null) { // initial set
            bitval = parseInt(csv[6]); // was 'var bitval = csv[6]'
        }
        // your load code
        bitval ^= 1; // toggle
    });
});

As Artjom mentioned, you need to convert it to a number since .split() returns strings. 如Artjom所述,您需要将其转换为数字,因为.split()返回字符串。

var bitval = Number(csv[6]);

You can do the XOR ing in the same step: 您可以在同一步骤中执行XOR运算

var bitval = Number(csv[6]) ^ 1;

In the case that the value cannot be converted to a number, the Number function will return NaN (not-a-number). 在无法将值转换为数字的情况下,Number函数将返回NaN (非数字)。 NaN ^ 1 equals 1. If that result is undesirable, you can do a type check: NaN ^ 1等于1。如果该结果不理想,则可以进行类型检查:

var bitval = Number(csv[6]);
if (isNaN(bitval)) {
    bitval = ...;
}

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

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