2016-08-13 75 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 [数字]“数”的值。 –