使用参数提交表单后的Javascript重定向url

时间:2018-10-09 14:46:55

标签: javascript forms

在重定向到子域时遇到麻烦。我的目标是将总价值发送到我的子域。让我告诉我我的表格

<form name="myform" id="myform">
   <div class="input-field">
      <p class="flow-text">Item yg dipesan...</p>
      <p>
         <label>
         <input type="checkbox" name="chkall" onclick="checkAll(this)">
         <span>Pilih Semua</span>
         </label>
      </p>
      <p>
         <label>
         <input type="checkbox" name="chkbox" value="10000">
         <span>Item 1</span>
         </label>
      </p>
      <p>
         <label>
         <input type="checkbox" name="chkbox" value="20000">
         <span>Item 2</span>
         </label>
      </p>
      <p>
         <label>
         <input type="checkbox" name="chkbox" value="30000">
         <span>Item 3</span>
         </label>
      </p>
      <p>
         <label>
         <input type="checkbox" name="chkbox" value="40000">
         <span>Item 4</span>
         </label>
      </p>
   </div>
   <div class="input-field">
      <button class="btn" type="button" onclick="getSum()">Submit</button>
   </div>
</form>

这是js

 function getSum() {
    totalvalue = 0
    obj = document.myform.chkbox
    for (i = 0; i < obj.length; i++) {
        if (obj[i].checked) {
            totalvalue += parseInt(obj[i].value)
        }

    }

    if (totalvalue == 0) {
        alert("Please select atleast one product!!");
    } else {
        var url= "subdomain.domainname.com/cgi-bin/sale.cgi?cashflow="+totalvalue+"&secret=secretword"; 
        window.location.href = url;
    }

}

function checkAll(obj) {
    if (obj.checked) {
        obj = document.myform.chkbox
        for (i = 0; i < obj.length; i++) {
            obj[i].checked = true
        }
    } else {
        obj = document.myform.chkbox
        for (i = 0; i < obj.length; i++) {
            obj[i].checked = false
        }
    }

}

让我说我的域名为domainname.com,在按下“提交”按钮后,我尝试重定向到subdomain.domainname.com/cgi-bin/sale.cgi?cashflow="+totalvalue+"&secret=secretword。我希望最终的网址看起来像这样subdomain.domainname.com/cgi-bin/sale.cgi?cashflow=100000&secret=secretword

当前结果显示最终的网址,例如domainname.com/subdomain.domainname.com/cgi-bin/sale.cgi?cashflow=100000&secret=secretword

1 个答案:

答案 0 :(得分:3)

您的location.href重定向到子URL。只需添加http://即可进行绝对重定向:

var url= "http://subdomain.domainname.com/cgi-bin/sale.cgi?cashflow="+totalvalue+"&secret=secretword";