我有一个付款表单,用户将在输入文本字段中输入金额。我有另一个相同形式的文本字段,其中将自动写入用户给出的金额的字数。但我有问题,我如何提取字段的值,并将其传递给将转换值的函数。请帮我。 这是用户将提供输入的字段。
func webViewDidFinishLoad(webView: UIWebView) {
let webString = webView.request?.URL?.absoluteString
if (webString! as NSString).containsString("PaymentCCAvenue/ReturnAPI") {
let html = webView .stringByEvaluatingJavaScriptFromString("document.documentElement.outerHTML")
if (html! as NSString).containsString("Success") {
}
else if (html! as NSString).containsString("Failure") {
}
else {
}
}
}
我有这个字段,价格应自动写入。我尝试了很多方法,但都失败了。
<div class="control-group form-group" style="padding-left:20px; padding-right: 20px;">
<div class="controls">
<label>Amount</label>
<input type="email" class="form-control" name="amount" id="amount" placeholder="Please enter amount"required/>
</div>
</div>
答案 0 :(得分:0)
已更新尝试此
function convertNumberToWords(amount) {
var words = new Array();
words[0] = '';
words[1] = 'One';
words[2] = 'Two';
words[3] = 'Three';
words[4] = 'Four';
words[5] = 'Five';
words[6] = 'Six';
words[7] = 'Seven';
words[8] = 'Eight';
words[9] = 'Nine';
words[10] = 'Ten';
words[11] = 'Eleven';
words[12] = 'Twelve';
words[13] = 'Thirteen';
words[14] = 'Fourteen';
words[15] = 'Fifteen';
words[16] = 'Sixteen';
words[17] = 'Seventeen';
words[18] = 'Eighteen';
words[19] = 'Nineteen';
words[20] = 'Twenty';
words[30] = 'Thirty';
words[40] = 'Forty';
words[50] = 'Fifty';
words[60] = 'Sixty';
words[70] = 'Seventy';
words[80] = 'Eighty';
words[90] = 'Ninety';
amount = amount.toString();
var atemp = amount.split(".");
var number = atemp[0].split(",").join("");
var n_length = number.length;
var words_string = "";
if (n_length <= 9) {
var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
var received_n_array = new Array();
for (var i = 0; i < n_length; i++) {
received_n_array[i] = number.substr(i, 1);
}
for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
n_array[i] = received_n_array[j];
}
for (var i = 0, j = 1; i < 9; i++, j++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
if (n_array[i] == 1) {
n_array[j] = 10 + parseInt(n_array[j]);
n_array[i] = 0;
}
}
}
value = "";
for (var i = 0; i < 9; i++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
value = n_array[i] * 10;
} else {
value = n_array[i];
}
if (value != 0) {
words_string += words[value] + " ";
}
if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Crores ";
}
if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Lakhs ";
}
if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Thousand ";
}
if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
words_string += "Hundred and ";
} else if (i == 6 && value != 0) {
words_string += "Hundred ";
}
}
words_string = words_string.split(" ").join(" ");
}
return words_string;
}
&#13;
<div class="control-group form-group" style="padding-left:20px; padding-right: 20px;">
<div class="controls">
<label>Amount</label>
<input type="email" class="form-control" name="amountInWords" id="amountInWords" placeholder="Amount in words" onkeyup="amount.innerHTML=convertNumberToWords(this.value)">
</div>
</div>
<div id="amount"></div>
&#13;
您的问题似乎是盲目的,请展示您的示例代码,无论如何您可以通过Jquery来完成,
获取该字段值的Jquery:
var ExtractedValue = $('#YourFieldID').val();
在使用之前必须包含Jquery库。
答案 1 :(得分:0)
使用此插件将值转换为金额。:http://www.jqueryscript.net/text/jQuery-Plugin-To-Convert-Numbers-Into-Words-num2words.html
以下是此插件的示例代码。
$(document).ready(function() {
$('#num').focus();
$('#demo').num2words();
});
$('#demo').num2words({
units: [ "", "One", "Two", "Three", "Four", "Five", "Six","Seven", "Eight", "Nine", "Ten" ],
teens: [ "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen","Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty" ],
tens: [ "", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty","Seventy", "Eighty", "Ninety" ],
othersIntl: [ "Thousand", "Million", "Billion", "Trillion" ]
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="https://www.jqueryscript.net/demo/jQuery-Plugin-To-Convert-Numbers-Into-Words-num2words/jquery.num2words.js"></script>
<div id="demo">
Enter amount: $ <input id="num" type="text">
<input id="trans" type="button" value="Convert to words">
<div></div>
</div>
&#13;
希望这个答案对你有所帮助。