2016-08-13 106 views
-1
<form id="contact-form"> 
    <input type="text" value="100" id="number" name="number" /> 
    <input type="hidden" value="00" id="decimal" name="decimal" /> 
    <input type="submit" name="submit-form" /> 
</form> 
<script> 
    var form = document.getElementById('#contact-form'); 
     form.addEventListener("submit", function() { 
      var input = document.createElement('number'); 
      input.type = 'text'; 
      input.name = 'decimal'; 
      input.value = '00'; 
      this.appendChild(input); 
     }, true); 
</script> 

//我希望它提交表單之前小數「00」追加到輸入的號碼。如何在提交前追加隱藏字段值以輸入文本字段?

//我想要的結果爲= 10000

+0

你的腳本是無效的。 var form = document.getElementById('#contact-form');應該沒有哈希。或者應該使用querySelector。 – 2016-08-13 07:24:48

回答

0
form.addEventListener("submit", function() { 
      var decimal_val= document.getElementByName('decimal').value; 
      var number= document.getElementByName('number').value; 
      number = decimal_val+number; 
     }, true); 
+0

雖然這個代碼片斷可以解決的問題,包括[說明](http://meta.stackexchange.com/q/114762/305455)確實有助於提高您的文章質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。也請儘量不要使用解釋性註釋來擠佔代碼,因爲這會降低代碼和解釋的可讀性! – jmattheis

0

試試這個

<html> 
 
<head></head> 
 
<title></title> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
<body> 
 

 

 
<form id="contact-form"> 
 
    <input type="text" value="100" id="number" name="number" /> 
 
    <input type="hidden" value="00" id="decimal" name="decimal" /> 
 
    <input type="submit" name="submit-form" id="clickme" /> 
 
</form> 
 

 
</body> 
 

 
<script type="text/javascript"> 
 
    
 
    $(document).ready(function(){ 
 

 
     //in here ,first check the existing value with an alert when document is ready 
 
    \t \t var hiddenValue = $("#decimal").val(); 
 
    \t \t alert("before value :" + hiddenValue); 
 

 

 
     //in this part you can assign the new value 
 

 
    \t \t $("#clickme").click(function(){ 
 
    \t \t \t var newhiddenVal = "new decimal"; 
 
    \t \t \t $("#decimal").val(newhiddenVal); 
 
    \t \t \t var displaynewHiddenvalue = $("#decimal").val(); 
 
    \t \t \t alert(displaynewHiddenvalue); 
 
    \t \t }); 
 
    }); 
 

 

 
</script> 
 

 
</html>

注:明白了,首先它顯示了現有的值,然後在點擊按鈕,指定新的值,然後顯示。(有alsert顯示,因爲這樣你可以很容易地理解)

分配按鈕,在值,請單擊

var newhiddenVal = "new decimal"; 
$("#decimal").val(newhiddenVal); 

按鈕點擊上面做的。希望這會有所幫助。

得到10000作爲新值,在你點擊鏈接

$("#clickme").click(function(){ 
      var newhiddenVal = $("#number").val() +hiddenValue; 
      $("#decimal").val(newhiddenVal); 
      var displaynewHiddenvalue = $("#decimal").val(); 
      alert(displaynewHiddenvalue); 
     }); 
+0

謝謝你,但還在原地面臨的問題..我已經試過,但什麼是主要的事情是...的終值即(1000)需要在「數量」值綁定。 –

+0

你的意思是你需要它作爲一個整數 –

+0

是..實際上是新價值「1000」必須在action_page.php [數字]「數」的值。 –