更新val输入在第一次击中时保持不变,适用于下次击中

时间:2016-12-11 06:55:22

标签: javascript jquery

我刚修改了简单的dinamically onChange代码,并在select选项下添加了一些输入文本。在它下面的3个输入文本,很快完成一些修改后我试试它运行。然后为什么第三次输入文本不变在第一次点击时,它仅适用于下次点击。

假设我们选择一个日期:17/08/1945(datepicker inputtext)
第一个输入字段仅显示日期名称:星期五 第二个输入字段返回值月份:8月份 用文字写的第3个输入日期:十七月十八日一九九四年 我知道这并不是一个用英语显示日期的非常有用的方式,但在印度尼西亚,用一个词来写日期,通常用于信用协议或协议移交和法律要求。 /> 我正在制作一个小应用程序,并在菜单上提供了一个自动打印协议书的功能。这就是为什么我需要用文字写日期的原因。
使用国际语言替换了片段和jsfiddle。



$( function() {
$( "#pickyDate" ).datepicker({format: "dd/mm/yyyy"});
daylocal = ['Minggu', 'Senin', 'Selasa', 'Rabu', 'Kamis', 'Jumat', 'Sabtu' ];
monthlocal = ['Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni','Juli','Agustus','September','Oktober','November','Desember' ];

 

$('#pickyDate').datepicker()
.on("change", function () {    

 var today = new Date($('#pickyDate').datepicker('getDate'));
 var date = today.getDate();
 var daysnumber = today.getDay();
 var monthnumber = today.getMonth(); 
 var years = today.getFullYear(); 
     

 numbers    = $('#dateinword').val();
 var number   = new Array('0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0');
 var words    = new Array('','Satu','Dua','Tiga','Empat','Lima','Enam','Tujuh','Delapan','Sembilan');/*1 to 9 */
 var level = new Array('','Ribu','Juta','Milyar','Triliun'); /*hundred,thousand,billion,trillion */
 
 var length_numbers = numbers.length;
 
 /* long test numbers */
 if (length_numbers > 15) {
   sentences = "Out of Limit";
   return sentences;
 }
 
 /* get numbers set to array */
 for (i = 1; i <= length_numbers; i++) {
   number[i] = numbers.substr(-(i),1);
 }
 
 i = 1;
 j = 0;
 sentences = "";
 
 
 /* iteration array number */
 while (i <= length_numbers) {
 
   subsentences = "";
   words1 = "";
   words2 = "";
   words3 = "";
 
   /* hundred */
   if (number[i+2] != "0") {
     if (number[i+2] == "1") {
       words1 = "Seratus";     /*Seratus mean One hundred */
     } else {
       words1 = words[number[i+2]] + " Ratus";  /* Ratus mean hundred  */
     }
   }
 
   /* tens or dozen */
   if (number[i+1] != "0") {
     if (number[i+1] == "1") {
       if (number[i] == "0") {
         words2 = "Sepuluh";   /* sepuluh mean ten */ 
       } else if (number[i] == "1") {
         words2 = "Sebelas";  /* sebelas mean eleven */
       } else {
         words2 = words[number[i]] + " Belas"; /* >10 - 19 using suffix Belas */
       }
     } else {
       words2 = words[number[i+1]] + " Puluh"; /* puluh is suffix like ty in english [20,30,40,...90]   */
     }
   }
 
   /* single number */
   if (number[i] != "0") {
     if (number[i+1] != "1") {
       words3 = words[number[i]];
     }
   }
 
   /* zero cheking, add level */
   if ((number[i] != "0") || (number[i+1] != "0") || (number[i+2] != "0")) {
     subsentences = words1+" "+words2+" "+words3+" "+level[j]+" ";
   }
 
   /* join var sentences (as one blok 3 digit 000) into var sentences */
   sentences = subsentences + sentences;
   i = i + 3;
   j = j + 1;
 
 }
 
 /* replace Satu Ribu(one thousand) will be Seribu if needed */
 if ((number[5] == "0") && (number[6] == "0")) {
   sentences = sentences.replace("Satu Ribu","Seribu"); /* Ribu = thousand we use prefix se for one  */
 }
 
 //return sentences;

   //alert(local[today.getDay()]);
   //alert(kalimat);
   $('#daypk').val(daylocal[today.getDay()]);
   $('#dateinword').val(date);
   $('#worddate').val(sentences);

//document.getElementById("terbilang").innerHTML=kalimat;



});
});
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>


<div class="row">
    <div class="col-lg-12">
        
        <div class="col-lg-3">

			<div class="form-group">
			 <input type="text" class="form-control" placeholder="specify the date of the loan agreement" name="pickyDate" id="pickyDate" />
			</div>

			 <div class="form-group">
			 <input type="text" class="form-control" placeholder="name of the day" name="daypk" id="daypk" disabled/>
			 </div>

             <div class="form-group">
             <input type="text" class="form-control" placeholder="dateinword" name="dateinword" id="dateinword" disabled/>
             </div>

             <div class="form-group">
             <input type="text" class="form-control" placeholder="will be dd/mm/yy in word" name="worddate" id="worddate" disabled/> 
             </div>


		</div>
	</div>
</div>
&#13;
&#13;
&#13;

https://jsfiddle.net/ariowishnu/vLeqLkj6/1/

1 个答案:

答案 0 :(得分:0)

不幸的是,我可以说我不会讲印度尼西亚语,因此很难说出你的代码究竟打算做什么,以及为什么你的逻辑可能按照它的方式进行结构化。但是,根据我对代码的测试,我可能会给您一些指示,这可能会改变第一个change事件的结果:

  • 您在事件处理程序的开头设置了一些变量bilangan = $('#nominal').val()。这将在第一次迭代时始终为空字符串'',因为尚未向该字段输入任何内容。

  • 您的while循环基于变量panjang_bilangan = bilangan.length。该循环将永远不会第一次运行,因为bilangan是空字符串,因此长度为零。

  • 您的第三个输入(出现错误的输入)似乎完全基于名为kaLimat的变量进行更新。据我所知,唯一可以更新此变量以包含一些有意义值的地方都在while循环中,这就是为什么第一个事件kaLimat最终为空,而你的{ {1}}不会改变。

编辑:我认为我找到了解决方案

问题在于<input id="terbilang">。该行应该是:

numbers = $('#dateinwords').val()

...将数字设置为当月所选日期的字符串 - (&#34; date&#34;)。

当您最初设置变量时,我还会使用var numbers = date + '' 为您的一些变量添加前缀,以便它们不会泄漏到全局命名空间中。

我没有看到你的算法将日期变成字符串的正确性,因为我不完全理解应该用印度尼西亚语做的规则。如果代码段没有按预期运行,请告诉我。

<强>段:

&#13;
&#13;
var
&#13;
$(function() {
  daylocal = ['Minggu', 'Senin', 'Selasa', 'Rabu', 'Kamis', 'Jumat', 'Sabtu'];
  monthlocal = ['Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'];



  $('#pickyDate').datepicker({ format: 'dd/mm/yyyy' })
    .on("change", function() {

      var today = new Date($('#pickyDate').datepicker('getDate'));
      var date = today.getDate();
      var daysnumber = today.getDay();
      var monthnumber = today.getMonth();
      var years = today.getFullYear();


      var numbers = date + ''
      var number = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'];
      var words = new Array('', 'Satu', 'Dua', 'Tiga', 'Empat', 'Lima', 'Enam', 'Tujuh', 'Delapan', 'Sembilan'); /*1 to 9 */
      var level = new Array('', 'Ribu', 'Juta', 'Milyar', 'Triliun'); /*hundred,thousand,billion,trillion */

      var length_numbers = numbers.length;

      /* long test numbers */
      if (length_numbers > 15) {
        var sentences = "Out of Limit";
        return sentences;
      }

      /* get numbers set to array */
      for (i = 1; i <= length_numbers; i++) {
        number[i] = numbers.substr(-(i), 1);
      }

      i = 1;
      j = 0;
      sentences = "";


      /* iteration array number */
      while (i <= length_numbers) {

        var subsentences = "";
        words1 = "";
        words2 = "";
        words3 = "";

        /* hundred */
        if (number[i + 2] != "0") {
          if (number[i + 2] == "1") {
            words1 = "Seratus"; /*Seratus mean One hundred */
          } else {
            words1 = words[number[i + 2]] + " Ratus"; /* Ratus mean hundred  */
          }
        }

        /* tens or dozen */
        if (number[i + 1] != "0") {
          if (number[i + 1] == "1") {
            if (number[i] == "0") {
              words2 = "Sepuluh"; /* sepuluh mean ten */
            } else if (number[i] == "1") {
              words2 = "Sebelas"; /* sebelas mean eleven */
            } else {
              words2 = words[number[i]] + " Belas"; /* >10 - 19 using suffix Belas */
            }
          } else {
            words2 = words[number[i + 1]] + " Puluh"; /* puluh is suffix like ty in english [20,30,40,...90]   */
          }
        }

        /* single number */
        if (number[i] != "0") {
          if (number[i + 1] != "1") {
            words3 = words[number[i]];
          }
        }

        /* zero cheking, add level */
        if ((number[i] != "0") || (number[i + 1] != "0") || (number[i + 2] != "0")) {
          subsentences = words1 + " " + words2 + " " + words3 + " " + level[j] + " ";
        }

        /* join var sentences (as one blok 3 digit 000) into var sentences */
        sentences = subsentences + sentences;
        i = i + 3;
        j = j + 1;

      }

      /* replace Satu Ribu(one thousand) will be Seribu if needed */
      if ((number[5] == "0") && (number[6] == "0")) {
        sentences = sentences.replace("Satu Ribu", "Seribu"); /* Ribu = thousand we use prefix se for one  */
      }

      //return sentences;

      //alert(local[today.getDay()]);
      //alert(kalimat);
      $('#daypk').val(daylocal[today.getDay()]);
      $('#dateinword').val(date);
      $('#worddate').val(sentences);

      //document.getElementById("terbilang").innerHTML=kalimat;



    });
});
&#13;
&#13;
&#13;