最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove line breaks and white space from variable in jQuery - Stack Overflow

programmeradmin2浏览0评论

I found several answers about this (Remove ALL white spaces from text [duplicate])

However not one single answer has worked in my case, please take a look if you have a moment..

1st step the Mako & Python template: Why I have new lines and white space in the first space:

We're using Mako templates and Python to generate data in our views:

<!-- The Python def on the page that pulls in the correct id -->
<%def name="pull_id(contact)">
    % if "member" in contact:
        ${contact["member"]["id"]}
    % else:
        ${contact["id"]}
    % endif
</%def>

<%def name="render_contact_row(contact)">

    <!-- the def returns the id here -->
    <tr data-contact-id='${pull_id(contact)}'>

Originally I had the Python code directly in the <tr> tag however that generated visible line-breaks. Now using the <%def at least it keeps it all on 1 line, but there are still a couple of extra white spaces in the HTML



Now my jQuery:

$('.btn_hide').live("click", function(event) {

    // gets the id number from the data tag in html
    var $tr = $(this).closest("tr");
    var id = $tr.data('contact-id');

    // tried this
    id.replace(/ /g,'');

    // then this
    id.replace(/\s+/, "");

    // even this
    id.replace(/\s/g, "");

    // still prints out white space :'(
    console.log(id);

    //...
});

When it hits the console.log line chrome prints this out:

Obviously with line breaks and extra white space

Finally it hits Python again:

@view_config(route_name="contacts_hide", request_method='POST')
def hide(self):
    id = self.param("id")
    if id is None:
        id = self.request.body
        if id.isdigit() is True:
            id = int(id)
        if id is None:
            raise Exception("The contact id parameter cannot be null!")

I've been having problems with self.param, so it will skip that and hit the id = self.request.body line.

And of course pulls in the line breaks and extra white space :'(

Please help!

I found several answers about this (Remove ALL white spaces from text [duplicate])

However not one single answer has worked in my case, please take a look if you have a moment..

1st step the Mako & Python template: Why I have new lines and white space in the first space:

We're using Mako templates and Python to generate data in our views:

<!-- The Python def on the page that pulls in the correct id -->
<%def name="pull_id(contact)">
    % if "member" in contact:
        ${contact["member"]["id"]}
    % else:
        ${contact["id"]}
    % endif
</%def>

<%def name="render_contact_row(contact)">

    <!-- the def returns the id here -->
    <tr data-contact-id='${pull_id(contact)}'>

Originally I had the Python code directly in the <tr> tag however that generated visible line-breaks. Now using the <%def at least it keeps it all on 1 line, but there are still a couple of extra white spaces in the HTML



Now my jQuery:

$('.btn_hide').live("click", function(event) {

    // gets the id number from the data tag in html
    var $tr = $(this).closest("tr");
    var id = $tr.data('contact-id');

    // tried this
    id.replace(/ /g,'');

    // then this
    id.replace(/\s+/, "");

    // even this
    id.replace(/\s/g, "");

    // still prints out white space :'(
    console.log(id);

    //...
});

When it hits the console.log line chrome prints this out:

Obviously with line breaks and extra white space

Finally it hits Python again:

@view_config(route_name="contacts_hide", request_method='POST')
def hide(self):
    id = self.param("id")
    if id is None:
        id = self.request.body
        if id.isdigit() is True:
            id = int(id)
        if id is None:
            raise Exception("The contact id parameter cannot be null!")

I've been having problems with self.param, so it will skip that and hit the id = self.request.body line.

And of course pulls in the line breaks and extra white space :'(

Please help!

Share Improve this question edited May 23, 2017 at 12:05 CommunityBot 11 silver badge asked Mar 20, 2014 at 16:09 Leon GabanLeon Gaban 39.1k122 gold badges349 silver badges550 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

Any of your examples will work if you assign the filtered value back to the variable:

var id = $tr.data('contact-id');
id = id.replace(/ /g, '');

However I'd remend you to use $.trim method instead:

var id = $.trim( $tr.data('contact-id') );

It will remove the spaces from the start and from the end of the value.

Finally Python has strip method, which does exactly the same:

id = id.strip()
发布评论

评论列表(0)

  1. 暂无评论