JavaScript chop/slice/trim off last character in string

I have a string, 12345.00 , and I would like it to return 12345.0 . I have looked at trim , but it looks like it is only trimming whitespace and slice which I don't see how this would work. Any suggestions? You can use the substring function: var str = "12345.00"; str = str.substring(0, str.length - 1); // "12345.0" This is the accepted answer, but as per the conversations below, the s

JavaScript chop / slice /修剪字符串中的最后一个字符

我有一个字符串, 12345.00 ,我希望它返回12345.0 。 我看过trim ,但它看起来只是修剪空白和slice ,我不明白这是如何工作的。 有什么建议么? 您可以使用子字符串函数: var str = "12345.00"; str = str.substring(0, str.length - 1); // "12345.0" 这是被接受的答案,但根据下面的对话,片语法更清晰: var str = "12345.00"; str = str.slice(0, -1); // "12345.0" 你可以使用切片! 你只需要确保你知道如何使

When are you supposed to use escape instead of encodeURI / encodeURIComponent?

When encoding a query string to be sent to a web server - when do you use escape() and when do you use encodeURI() or encodeURIComponent() : Use escape: escape("% +&="); OR use encodeURI() / encodeURIComponent() encodeURI("http://www.google.com?var1=value1&var2=value2"); encodeURIComponent("var1=value1&var2=value2"); escape() Special characters are encoded with the exceptio

你什么时候应该使用escape来代替encodeURI / encodeURIComponent?

编码发送到Web服务器的查询字符串时 - 何时使用escape()以及何时使用encodeURI()或encodeURIComponent() : 使用转义: escape("% +&="); 要么 使用encodeURI()/ encodeURIComponent() encodeURI("http://www.google.com?var1=value1&var2=value2"); encodeURIComponent("var1=value1&var2=value2"); 逃逸() 特殊字符的编码除以下外:@ * _ + - 。/ 字符的十六进制形式(代码单元值为0xFF或更小

How do you disable input value on web form field / input tag?

How do you disable Autocomplete in the major browsers for a specific input (or form field)? <input type="text" id="fullname" name="fullname" class="form-control" data-required="true" value="<?php echo $_POST['fullname']?>" > When open this form i see value in this input but i dont insert any value. Just use the autocomplete attribute: <input type="text" autocomplete=

如何禁用Web表单域/输入标签的输入值?

如何在主要浏览器中为特定输入(或表单域)禁用自动完成功能? <input type="text" id="fullname" name="fullname" class="form-control" data-required="true" value="<?php echo $_POST['fullname']?>" > 当打开这个表单时,我在这个输入中看到了值,但是我没有插入任何值。 只需使用autocomplete属性即可: <input type="text" autocomplete="off"/> “当文本输入是一次性和唯一的时候,这

jQuery disable/enable submit button

I have this html: <input type="text" name="textField" /> <input type="submit" value="send" /> How can I do something like this: When the text field is empty the submit should be disabled (disabled="disabled"). When something is typed in the text field to remove the disabled attribute. If the text field becomes empty again(the text is deleted) the submit button should

jQuery禁用/启用提交按钮

我有这个html: <input type="text" name="textField" /> <input type="submit" value="send" /> 我该如何做这样的事情: 当文本字段为空时,应该禁用提交(disabled =“disabled”)。 在文本字段中输入某些内容以删除禁用的属性时。 如果文本字段再次变空(文本被删除),则应该再次禁用提交按钮。 我尝试了这样的事情: $(document).ready(function(){ $('input[type="submit"]').attr('disabled','d

HTML form readonly SELECT tag/input

According to HTML specs, the select tag in HTML doesn't have a readonly attribute, only a disabled attribute. So if you want to keep the user from changing the dropdown, you have to use disabled . The only problem is that disabled HTML form inputs don't get included in the POST / GET data. What's the best way to emulate the readonly attribute for a select tag, and still get the P

HTML表单只读选择标记/输入

根据HTML规范,HTML中的select标签没有readonly属性,只有一个disabled属性。 所以如果你想让用户不想改变下拉菜单,你必须使用disabled 。 唯一的问题是禁用的HTML表单输入不会包含在POST / GET数据中。 模拟select标签的readonly属性的最好方法是什么,仍然可以获取POST数据? 您应该保持disabled select元素,但也要添加具有相同名称和值的另一个隐藏input 。 如果您重新启用SELECT,则应将其值复制到onchange事件中

Wrapping a DOM element inside a JavaScript object

I've noticed a common pattern in the JavaScript I've been writing and was wondering if there is already a pattern out there that defines something similar as best practice? Essentially, it's how to get a DOM element and wrap it inside / associate it with a JavaScript object. Take this example, where you need a filter in your web app. Your page looks like this: <html> <hea

包装JavaScript对象内的DOM元素

我注意到了我一直在写的JavaScript中的一种常见模式,并且想知道是否已经有一种模式定义类似最佳实践的模式? 从本质上讲,它是如何获得一个DOM元素并将其包装/关联到一个JavaScript对象。 以此示例为例,您需要在Web应用程序中使用过滤器。 你的页面如下所示: <html> <head></head> <body> <div id="filter"></div> </body> </html> 然后你会像这样包装元素: var m

jQuery Birthday Picker dealing with form refresh

I'm using an excellent jquery plugin for Birthday Date picking for my web form. Demo here: http://abecoffman.com/stuff/birthdaypicker/ The problem I'm dealing with is on a form validation. My form validation redirect back to the same page and I'll lose the date that was picked. Is there any way to add an option to the javascript of the date picker: http://abecoffman.com/stuff/b

处理表单刷新的jQuery生日选择器

我为我的网页表单使用生日日期选择的优秀jquery插件。 在这里演示:http://abecoffman.com/stuff/birthdaypicker/ 我正在处理的问题是在表单验证上。 我的表单验证重定向回到同一页面,我将失去被挑选的日期。 有没有办法给日期选择器的javascript添加一个选项:http://abecoffman.com/stuff/birthdaypicker/bday-picker.js 以便我可以设置“默认选择日期”。 该配置可以工作,如: $("#picker1").birthdaypicker({ ch

How to compare arrays in JavaScript?

I'd like to compare two arrays... ideally, efficiently. Nothing fancy, just true if they are identical, and false if not. Not surprisingly, the comparison operator doesn't seem to work. var a1 = [1,2,3]; var a2 = [1,2,3]; console.log(a1==a2); // Returns false console.log(JSON.stringify(a1)==JSON.stringify(a2)); // Returns true JSON encoding each array does, but is there a faster

如何比较JavaScript中的数组?

我想比较两个阵列...理想,有效。 没有什么花哨,只要它们是相同的就是true ,如果不相同则是false 。 毫不奇怪,比较运算符似乎不起作用。 var a1 = [1,2,3]; var a2 = [1,2,3]; console.log(a1==a2); // Returns false console.log(JSON.stringify(a1)==JSON.stringify(a2)); // Returns true 每个数组都有JSON编码,但是有没有更快或更好的方法来简单比较数组而无需遍历每个值? 比较数组,循环遍历它们并比较每

Switch statement multiple cases in JavaScript

I need multiple cases in switch statement in JavaScript, Something like: switch (varName) { case "afshin", "saeed", "larry": alert('Hey'); break; default: alert('Default case'); break; } How can I do that? If there's no way to do something like that in JavaScript, I want to know an alternative solution that also follows DRY concept. Use the fall-throu

在JavaScript中切换语句多个案例

我需要JavaScript中的switch语句中的多个案例,如: switch (varName) { case "afshin", "saeed", "larry": alert('Hey'); break; default: alert('Default case'); break; } 我怎样才能做到这一点? 如果在JavaScript中没有办法做这样的事情,我想知道一个也遵循DRY概念的替代解决方案。 使用switch语句的fall-through功能。 一个匹配的情况会一直运行,直到找到一个break (或者sw

Choosing the most colorful color in javascript

I have a group of colors and I want to find their relative colorfulness. Could anyone show me an example in javascript? Many thanks! // You can compare colors by their saturation and brightness- function rgbtoHsv(rgb){ var c= rgb.match(/d+/g), r= c[0]/255, g= c[1]/255, b= c[2]/255, max= Math.max(r, g, b), min= Math.min(r, g, b), h= 0, s= 0, v= max; if(max!= min){

在javascript中选择最丰富多彩的颜色

我有一组颜色,我想找到他们的相对丰富多彩。 任何人都可以在JavaScript中显示一个例子吗? 非常感谢! //你可以通过饱和度和亮度来比较颜色 - function rgbtoHsv(rgb){ var c= rgb.match(/d+/g), r= c[0]/255, g= c[1]/255, b= c[2]/255, max= Math.max(r, g, b), min= Math.min(r, g, b), h= 0, s= 0, v= max; if(max!= min){ var d= max-min; s= d/max; switch(max){