2017-02-04 49 views
0

我正在嘗試使用附加到URL的URL和使用更改的URL的按鈕來更改重定向鏈接。我仍然是JavaScript的初學者,並且對於我出錯的地方有點失落。更改重定向位置onClick。

我的代碼:

<html> 

<form> 

<div class="element1">" 
<input type="checkbox" name="name1" id="id1" value="val1" class="hidden" ></label></div> 

<div class="element2">" 
<input type="checkbox" name="name2" id="id2" value="val2" class="hidden" ></label></div> 

<div class="element3">" 
<input type="checkbox" name="name3" id="id3" value="val3" class="hidden" ></div> 


<div class="element00"> 
<input type="button"onclick="myFunction(calculatedUrl());" name="button1" id="itemA" value="valA" ></label></div> 

</form> 

<script> 
function calculatedUrl(){ 
    var totalString="" 
    var link1="some text" 
    var link2="more text" 
    var link3="something else" 

    if(document.getElementById("id1").checked == true){ 
    var totalString = totalString.concat(link1); 

} 

if(document.getElementById("id2").checked == true){ 
     var totalString = totalString.concat(link2); 


} 

if(document.getElementById("id3").checked == true){ 
     var totalString = totalString.concat(link3); 

} 
    return totalString 

} 

function myFunction(id){ 
var original="http://myoriginalurl" 
var addition=id 
var new=original.concat(addition) 




//for testing 
    window.alert(new) 

    document.getElementById("test1").innerHTML= new; 


//for the real redirecting not active for now 
//window.location=new 
          } 




</script> 


</html> 

回答

0

<html> 
 

 
<form> 
 

 
<div class="element1">" 
 
<input type="checkbox" name="name1" id="id1" value="val1" class="hidden" ></label></div> 
 

 
<div class="element2">" 
 
<input type="checkbox" name="name2" id="id2" value="val2" class="hidden" ></label></div> 
 

 
<div class="element3">" 
 
<input type="checkbox" name="name3" id="id3" value="val3" class="hidden" ></div> 
 

 

 
<div class="element00"> 
 
<input type="button"onclick="myFunction(calculatedUrl());" name="button1" id="itemA" value="valA" ></label></div> 
 

 
</form> 
 

 
<script> 
 
function calculatedUrl(){ 
 
    var totalString="" 
 
    var link1="some text" 
 
    var link2="more text" 
 
    var link3="something else" 
 

 
    if(document.getElementById("id1").checked == true){ 
 
    var totalString = totalString.concat(link1); 
 

 
} 
 

 
if(document.getElementById("id2").checked == true){ 
 
     var totalString = totalString.concat(link2); 
 

 

 
} 
 

 
if(document.getElementById("id3").checked == true){ 
 
     var totalString = totalString.concat(link3); 
 

 
} 
 
    return totalString 
 

 
} 
 

 
function myFunction(id){ 
 
var original="http://myoriginalurl" 
 
var addition=id 
 
console.log(id); 
 
var n =original.concat(addition) 
 

 

 

 

 
//for testing 
 
    window.alert(n) 
 

 
    document.getElementById("test1").innerHTML= n; 
 

 

 
//for the real redirecting not active for now 
 
//window.location=new 
 
          } 
 

 

 

 

 
</script> 
 

 

 
</html>

目前在您的代碼更改下面,newnewurl - new是一個保留字。

function myFunction(id){ 
var original="http://myoriginalurl" 
var addition=id 
var newurl =original.concat(addition) 
//for testing 
window.alert(newurl) 
document.getElementById("test1").innerHTML= newurl; 
//for the real redirecting not active for now 
//window.location=new 
} 

我沒有看到一個標籤與id=test1,所以你會在控制檯瀏覽器中得到空例外。根據該標籤添加標籤並進行測試。

我已經添加了一個片段,請檢查。

+0

我改變新NEWURL但仍然沒有改變。警告不出現和測試段落不更新。 –

+0

我已經添加了一個片段,請檢查點擊valA按鈕,您將得到警報 – Thennarasan

+0

我有一段id = test1我忘了將它粘貼在問題代碼中。它看起來像它沒有運行myFunction。它在計算Url()中的某個地方突破。感謝您的幫助! –

0

<html> 
 

 
<form> 
 

 
<div class="element1">" 
 
<input type="checkbox" name="name1" id="id1" value="val1" class="hidden" ></label></div> 
 

 
<div class="element2">" 
 
<input type="checkbox" name="name2" id="id2" value="val2" class="hidden" ></label></div> 
 

 
<div class="element3">" 
 
<input type="checkbox" name="name3" id="id3" value="val3" class="hidden" ></div> 
 

 

 
<div class="element00"> 
 
<input type="button"onclick="myFunction(calculatedUrl());" name="button1" id="itemA" value="valA" ></label></div> 
 

 
</form> 
 

 
<script> 
 
function calculatedUrl(){ 
 
    var totalString="" 
 
    var link1="some text" 
 
    var link2="more text" 
 
    var link3="something else" 
 

 
    if(document.getElementById("id1").checked == true){ 
 
    var totalString = totalString.concat(link1); 
 

 
} 
 

 
if(document.getElementById("id2").checked == true){ 
 
     var totalString = totalString.concat(link2); 
 

 

 
} 
 

 
if(document.getElementById("id3").checked == true){ 
 
     var totalString = totalString.concat(link3); 
 

 
} 
 
    return totalString 
 

 
} 
 

 
function myFunction(id){ 
 
var original="http://myoriginalurl" 
 
var addition=id 
 
console.log(id); 
 
var n =original.concat(addition) 
 

 

 

 

 
//for testing 
 
    window.alert(n) 
 

 
    document.getElementById("test1").innerHTML= n; 
 

 

 
//for the real redirecting not active for now 
 
//window.location=new 
 
          } 
 

 

 

 

 
</script> 
 

 

 
</html>