English 中文(简体)
我如何在贾瓦里将“9.61”改为“9:61”?
原标题:How do I do string replace in JavaScript to convert ‘9.61’ to ‘9:61’?

鉴于代码行

var value = $("#text").val();

www.un.org/spanish/ga/president 我如何利用 Java来取代本文的功能?

最佳回答

希望:

var value = $("#text").val(); // value = 9.61 use $("#text").text() if you are not on select box...
value = value.replace(".", ":"); // value = 9:61
// can then use it as
$("#anothertext").val(value);

更新,以反映目前版本的j Query。 这里还有许多答案最适合这种情况。 作为发展者,你们需要知道什么。

Replace all occurrences

替换多种特性时使用某些东西:.replace(/&/g,“-”。 在此,我替换了所有<代码>&。 <代码>->>。

<>说明>——您可能需要添加方括号,以避免出现错误——<代码>,replace(/[+]/g,“

Dante Cullari

问题回答

或许,这样做的最可取方式就是采取一个步骤。 见val(

$("#text").val(function(i, val) {
  return val.replace( . ,  : );
});

相比之下:

var val = $("#text").val();
$("#text").val(val.replace( . ,  : ));

参考:

.val( function(index, value) )

function(index, value)A function returning the value to set.

This method is typically used to set the values of form fields. For <select multiple="multiple"> elements, multiple s can be selected by passing in an array.

The .val() method allows us to set the value by passing in a function. As of jQuery 1.4, the function is passed two arguments, the current element s index and its current value:

$( input:text.items ).val(function(index, value) {
  return value +     + this.className;
});

This example appends the string " items" to the text inputs values.

这就需要j Query 1.4+。

简单一行:

$("#text").val( $("#text").val().replace(".", ":") );

可以通过定期的 Java印功能replace(<>/code>进行。

value.replace(".", ":");

您可使用 Java文功能,如取代,并可在括号中总结 j法:

var value = ($("#text").val()).replace(".", ":");
$("#text").val(function(i,v) { 
   return v.replace(".", ":"); 
});
(9.61 + "").replace( . , : )

或者,如果贵国的<代码>9.61已经作了说明:

"9.61".replace( . , : )




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签