简体   繁体   中英

Remove string from string jquery

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

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

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();             

            });
        });

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

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

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

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

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 :

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, eg, "originalString = originalString.blah(...);`.

More to explore:

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();             

            });
        });

To remove the checkbox_ part, you can simply do this:

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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