Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 要获取文本框值吗_Javascript_Jquery_Forms - Fatal编程技术网

Javascript 要获取文本框值吗

Javascript 要获取文本框值吗,javascript,jquery,forms,Javascript,Jquery,Forms,我有两个文本框,其值如下: 312,315 我想获得一个包含所有值的字符串,例如:312315313跳过两个字段中的现有值。 我的代码: var firstbox = $("#firstbox").val(); var secondbox = $("#secondbox").val(); var newvalue = $(firstbox).not(secondbox).get(); console.log(newvalue); 但它不起作用,如何使用JQuery获得所需的输出?

我有两个文本框,其值如下:

312,315


我想获得一个包含所有值的字符串,例如:
312315313
跳过两个字段中的现有值。

我的代码:

var firstbox = $("#firstbox").val();
var secondbox = $("#secondbox").val();
var newvalue = $(firstbox).not(secondbox).get();
    console.log(newvalue);
但它不起作用,如何使用JQuery获得所需的输出?

谢谢。

也许这会给你一个正确方向的提示:

// get comma seperated list of all values
var allValues = $('#firstbox').val() + ',' + $('#secondbox').val();

// make an array out of them
var allValuesArray = allValues.split(',');


// sort out repeated values
// by creating a new array 'distinctValues'
var distinctValues = [],
    currentValue,
    valuesLookup = {};

for (var i = allValuesArray.length - 1; i >= 0; i--) {
    currentValue = allValuesArray[i];

    if (!valuesLookup[currentValue]) {
        valuesLookup[currentValue] = true;
        distinctValues.push(currentValue);
    }
}


// output the result to the console
console.log(distinctValues.join(','));

你可以用逗号将两者连接起来。 然后会有一个逗号分隔的字符串,这样就可以在逗号处拆分,删除任何重复的值,然后重新连接其余的值

大概是这样的:

var firstbox = $("#firstbox").val(),
    secondbox = $("#secondbox").val(),
    boxes = firstbox + "," + secondbox,
    arr = boxes.split(","),
    res = [];

$.each(arr, function(i, el){
    if($.inArray(el, res) === -1){
        res.push(el);
    }
});

$("#res").html(res.join(",").replace(/(^,)|(,$)/g, ""));

如果一个文本框没有值,则。。。表示一个文本框为空,然后显示212215,最后我更新了我的小提琴和上面的演示中的额外逗号来说明这一点
var firstbox = $("#firstbox").val(),
    secondbox = $("#secondbox").val(),
    boxes = firstbox + "," + secondbox,
    arr = boxes.split(","),
    res = [];

$.each(arr, function(i, el){
    if($.inArray(el, res) === -1){
        res.push(el);
    }
});

$("#res").html(res.join(",").replace(/(^,)|(,$)/g, ""));