1

I am having to pickup from where someone in the business left off many years ago with an aging texting system.

It was built using ASP classic and sends a string to an API that then texts out, all this is neither here nor there. The problem i have is no JS experience, I am am a SQL Developer and did a little bit of ASP Classic (VBScript) years ago.

This piece of JScript picks up information from several form boxes and then places them in a string which is then passed to variable on a processing page to text out. The fields 'QValue, Indemnity and Excess' are all numeric. The Cover is text and it is replacing the cover text with 'NaN' now I understand this is for 'Not A Number' well that is exactly what it is, not a number but I want the text string.

Here is the snippet of code in question:

<script type="text/javascript">
function changeMessageText()
{

var messagetxt = document.getElementById('message').value

var QValue = document.getElementById('QValue').value
var Cover = document.getElementById('Cover').value
var Excess = document.getElementById('Excess').value
var Indem = document.getElementById('Indemnity').value

var messagetxt=messagetxt.replace("[QValue]", + QValue)
var messagetxt=messagetxt.replace("[Cover]", + Cover2)
var messagetxt=messagetxt.replace("[Excess]", + Excess)
var messagetxt=messagetxt.replace("[Indem]", + Indem)

document.getElementById('messageText').innerHTML = messagetxt;

}
</script>

Cheers.

4

3 回答 3

5

当你这样做时string.replace(searchvalue,newvalue),就不需要+newValue

var messagetxt=messagetxt.replace("[QValue]", QValue)
//cover or cover2 whichever appropriate
var messagetxt=messagetxt.replace("[Cover]",  Cover) 

var messagetxt=messagetxt.replace("[Excess]",  Excess)
var messagetxt=messagetxt.replace("[Indem]",  Indem)
于 2013-04-25T10:58:53.357 回答
1

在读取输入值并将其存储在Cover变量中的替换中使用Cover2是否正常?

这是两个不同的变量,根据您提供的代码,我们只能假设 Cover2 是用 NaN 初始化的(可能不是这种情况,它可能是复制/粘贴错误)。

于 2013-04-25T11:04:16.390 回答
0

Here is how you do it:

var messagetxt = document.getElementById('message').value;

var QValue = document.getElementById('QValue').value
var Cover = document.getElementById('Cover').value

var messagetxt=messagetxt.replace("[QValue]", QValue)
var messagetxt=messagetxt.replace("[Cover]", Cover)

document.getElementById('messagetxt').innerHTML = messagetxt;

Here is a working example of this: http://jsfiddle.net/F24cr/

Enjoy

于 2013-04-25T11:09:59.340 回答