我创建了单个输入的前一个代码。我使用了这个代码“upfrontmippc”的名称。现在我有四个输入字段相同的功能?这里我的代码更新了。此代码在我使用单个输入字段时工作。现在我更改了四个输入字段。分配此函数四个输入字段不起作用
<input name="upfrontmippc" id="upfrontmippc" type="text" size="3" maxlength="3"
onChange="javascript:upfrontMIPPcChanged(true)" />
我在新设计中分配了四个字段:此值在我的新代码中分配四次?
<div class="col-md-2 padding-Zero">
<input name="upfrontmippc" class="txt" type="text" size="8" maxlength="8" onChange="javascript:upfrontMIPPcChanged(true)" />
</div>
我的剧本:
/*state holders*/
/*WARNING!! this script must be above all other js scripts*/
var userChangedUpfrontMIPPc_=false;
var userChangedUpfrontMIPAmt_=false;
var userChangedMonthlyMIP_=false;
var userChangedDownPaymentPc_=false;
var userChangedDownPaymentAmt_=false;
userChangedUpfrontMIP=false;
userChangedMonthlyMIP=false;
userChangedSSMonthlyMIP=false;
function userChangedUpfrontMIPPc(state)//state=true or false
{
userChangedUpfrontMIPPc_=state;
}
function userChangedUpfrontMIPAmt(state)//state=true or false
{
userChangedUpfrontAmt_=state;
}
function userChangedMonthlyMIP(state)//state=true or false
{
userChangedUpfrontMIP_=state;
}
function userChangedDownPaymentPc(state)//state=true or false
{
userChangedDownPaymentPc_=state;
}
function userChangedDownPaymentAmt(state)//state=true or false
{
userChangedDownPaymentAmt_=state;
}
function getMIP(str)
{
//get user input
ficorange=document.forms['frmCalculator'].elements['ficorange'].value;
//loantype=document.forms['frmCalculator'].elements['loantype'].value;
purchaseprice=document.forms['frmCalculator'].elements['purchaseprice'].value;
downpaymentpc=document.forms['frmCalculator'].elements['downpaymentpc'].value;
downpaymentamt=document.forms['frmCalculator'].elements['downpaymentamt'].value;
for (i=0;i<4;i++ )
{
if (document.forms['frmCalculator'].elements['loantype'][i].checked==true)
{
loantype=document.forms['frmCalculator'].elements.loantype[i].value;
}
}
//seconds and silent seconds
totalseconds_=0.0;
totalsilentseconds_=0.0;
secondV=document.forms['frmCalculator'].elements['second'].value;
if (secondV=="") { secondV="0"; }
secondV_=parseFloat(secondV);
secondT=document.forms['frmCalculator'].elements['secondtype'].value;
if (secondT=="normal") {totalseconds_+= secondV_;} else {totalsilentseconds_+= secondV_;}
thirdV=document.forms['frmCalculator'].elements['third'].value;
if (thirdV=="") { thirdV="0"; }
thirdV_=parseFloat(thirdV);
thirdT=document.forms['frmCalculator'].elements['thirdtype'].value;
if (thirdT=="normal") {totalseconds_+= thirdV_;} else {totalsilentseconds_+= thirdV_;}
fourthV=document.forms['frmCalculator'].elements['fourth'].value;
fourthT=document.forms['frmCalculator'].elements['fourthtype'].value;
if (fourthV=="") { fourthV="0"; }
fourthV_=parseFloat(fourthV);
if (fourthT=="normal") {totalseconds_+= fourthV_;} else {totalsilentseconds_+= fourthV_;}
fifthV=document.forms['frmCalculator'].elements['fifth'].value;
fifthT=document.forms['frmCalculator'].elements['fifthtype'].value;
if (fifthV=="") { fifthV="0"; }
fifthV_=parseFloat(fifthV);
if (fifthT=="normal") {totalseconds_+= fifthV_;} else {totalsilentseconds_+= fifthV_;}
sixthV=document.forms['frmCalculator'].elements['sixth'].value;
sixthT=document.forms['frmCalculator'].elements['sixthtype'].value;
if (sixthV=="") { sixthV="0"; }
sixthV_=parseFloat(sixthV);
if (sixthT=="normal") {totalseconds_+= sixthV_;} else {totalsilentseconds_+= sixthV_;}
q="ficorange="+ficorange;
q+="&loantype="+loantype;
q+="&purchaseprice="+purchaseprice;
q+="&downpaymentpc="+downpaymentpc;
q+="&downpaymentamt="+downpaymentamt;
q+="&seconds="+totalseconds_;
q+="&silentseconds="+totalsilentseconds_;
if (loantype=="" || purchaseprice=="" || (downpaymentpc =="" && downpaymentamt ==""))
{
//document.getElementById("txtHint").innerHTML= "";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// alert (xmlhttp.responseText);
var values=xmlhttp.responseText.split(",");
//alert (xmlhttp.responseText);
if (str!="loantype")
{
if (userChangedUpfrontMIP==false)
{
document.forms["frmCalculator"].elements["upfrontmippc"].value=values[0];
document.forms["frmCalculator"].elements["upfrontmipamt"].value="";
}
if (userChangedMonthlyMIP==false)
{
document.forms["frmCalculator"].elements["monthlymippc"].value=values[2];
document.forms["frmCalculator"].elements["monthlymipamt"].value="";
}
if (userChangedSSMonthlyMIP==false)
{
document.forms["frmCalculator"].elements["ssmonthlymippc"].value=values[6];
document.forms["frmCalculator"].elements["ssmonthlymipamt"].value="";
}
}
else
{
document.forms["frmCalculator"].elements["upfrontmippc"].value=values[0];
document.forms["frmCalculator"].elements["upfrontmipamt"].value="";
document.forms["frmCalculator"].elements["monthlymippc"].value=values[2];
document.forms["frmCalculator"].elements["monthlymipamt"].value="";
document.forms["frmCalculator"].elements["ssmonthlymippc"].value=values[6];
document.forms["frmCalculator"].elements["ssmonthlymipamt"].value="";
}
}
}
xmlhttp.open("GET","getMIPs.php?"+q,true);
xmlhttp.send();
}
现在我有四个输入字段如何在四个输入字段中分配此值? 如何使用四个输入字段使用相同的名称?
答案 0 :(得分:0)
您可以使用querySelectorAll并创建一个循环来分配每个值,如下所示:
var selectElement = document.querySelectorAll('input[name="upfrontmippc"]');
for (var i = 0; i <= selectElement.length; i++){
selectElement[i].value = "newValue";
}