Javascript 如何在jQuery中删除变量中的换行符和空格
我找到了关于这个()的几个答案 然而,在我的情况下,没有一个答案是有效的,如果您有时间,请看一看。Javascript 如何在jQuery中删除变量中的换行符和空格,javascript,jquery,python,whitespace,Javascript,Jquery,Python,Whitespace,我找到了关于这个()的几个答案 然而,在我的情况下,没有一个答案是有效的,如果您有时间,请看一看。 第一步Mako&Python模板:为什么我在第一个空格中有新行和空白: 我们使用Mako模板和Python在视图中生成数据: <!-- The Python def on the page that pulls in the correct id --> <%def name="pull_id(contact)"> % if "member" in contact:
第一步Mako&Python模板:为什么我在第一个空格中有新行和空白: 我们使用Mako模板和Python在视图中生成数据:
<!-- 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)}'>
当点击console.log行时,chrome会打印出以下内容:
显然是换行符和额外的空白最后它再次击中Python:
$('.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);
//...
});
@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!")
我一直在使用self.param时遇到问题,因此它将跳过该操作并点击id=self.request.body
行
当然,还会引入换行符和额外的空白:(
请提供帮助!如果将过滤后的值重新分配给变量,您的任何示例都将有效:
var id = $tr.data('contact-id');
id = id.replace(/ /g, '');
但是,我建议您使用$.trim
方法:
var id = $.trim( $tr.data('contact-id') );
它将从值的起点和终点删除空格
最后,Python有strip
方法,该方法的作用完全相同:
id = id.strip()
如果将过滤后的值重新分配给变量,则任何示例都有效:
var id = $tr.data('contact-id');
id = id.replace(/ /g, '');
但是,我建议您使用$.trim
方法:
var id = $.trim( $tr.data('contact-id') );
它将从值的起点和终点删除空格
最后,Python有strip
方法,该方法的作用完全相同:
id = id.strip()
谢谢!哈哈,是的,我不敢相信我把那根绳子弄乱了谢谢!哈哈,是的,我不敢相信我把那根绳子弄乱了