Javascript 从字符串 jquery 中删除字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10586930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:59:46  来源:igfitidea点击:

Remove string from string jquery

javascriptjquery

提问by user525146

Ignore my novice knowledge in jquery. I started to learn recently and I have a challenge in front of me. I have a checkbox with names checkbox_0, checkbox_1 and want to remove "checkbox_" from the strings so that I would use the 0, 1 in my loop to extract the data for that index. Thanks

忽略我在jquery中的新手知识。我最近开始学习,我面前有一个挑战。我有一个名为 checkbox_0、checkbox_1 的复选框,想从字符串中删除“checkbox_”,以便我在循环中使用 0, 1 来提取该索引的数据。谢谢

aData value alerts me the value checkbox_0, checkbox_1 etc. those are the checkboxes selected.

aData 值提醒我值 checkbox_0、checkbox_1 等。这些是选中的复选框。

submitButton.on("click", function() {
           $("Table :checked").each(function(e) {
                var iData =Table.fnGetData( this.parentNode);
                // Strip of the checkbox_ from the string
                for(var i=0; i<=iData.length; i++) {
                  aData = iData[i][7];
                }
                alert(aData);
                Table.fnDraw();             

            });
        });

回答by T.J. Crowder

This is just a JavaScript, not a jQuery, thing.

这只是一个 JavaScript,而不是 jQuery。

To remove the first occurence of the work "checkbox_":

要删除工作“checkbox_”的第一次出现:

var updatedString = originalString.replace("checkbox_", "");

Or if you know it'll always be in the form "checkbox_n"where nis a digit,

或者如果你知道它总是以数字的形式"checkbox_n"出现n

var updatedString = originalString.substring(9);

...which chops the first nine characters off the string.

...从字符串中删除前九个字符。

In either case, you end up with a string. If you want a number, you can use parseInt:

在任何一种情况下,您最终都会得到一个字符串。如果你想要一个数字,你可以使用parseInt

var updatedString = parseInt(originalString.replace("checkbox_", ""), 10);
// or
var updatedString = parseInt(originalString.substring(9), 10);

...or just put a +in front of it to cause an automatic cast (but note that in that case, both decimal and hex strings will be handled):

...或者只是+在它前面放一个来导致自动转换(但请注意,在这种情况下,十进制和十六进制字符串都将被处理):

var updatedString = +originalString.replace("checkbox_", "");
// or
var updatedString = +originalString.substring(9);


Note that I've written updatedString = originalString.blah(...);but of course you can replace your reference, e.g., "originalString = originalString.blah(...);`.

请注意,我已经写过,updatedString = originalString.blah(...);但您当然可以替换您的参考,例如,“originalString = originalString.blah(...);”。

More to explore:

更多探索:

回答by rabbidrabbit

submitButton.on("click", function() {
           $("Table :checked").each(function(e) {
                var iData =Table.fnGetData( this.parentNode);
                // Strip of the checkbox_ from the string
                for(var i=0; i<=iData.length; i++) {
                  aData = iData[i].replace("checkbox_", "");
                }
                alert(aData);
                Table.fnDraw();             

            });
        });

回答by D. Strout

To remove the checkbox_part, you can simply do this:

要移除checkbox_零件,您只需执行以下操作:

cbName=cbName.replace("checkbox_", "");

To do this for all your checkboxes inside the .each()loop:

要对.each()循环内的所有复选框执行此操作:

var cbIndex=this.name.replace("checkbox_", "");
//or...
var cbIndex=this.name.split("checkbox_").join("");

回答by gdoron is supporting Monica

There are many ways to do it, some of them:

有很多方法可以做到,其中一些:

$("table :checked").each(function() {
    var theNumber = this.name.replace(/\D/g, "");
    var theNumber = this.name.replace(/[^\d]/g, ""); // or this
    var theNumber = this.name.match(/\d/g).join();   // or this