2016-11-19 54 views
0

我想將選中的複選框值作爲URL參數添加到我的鏈接。 這是我在其他之中:使用javascript將複選框值添加到鏈接

<input id="box1" type="checkbox" class="checkbox" value="bx1=1"> <label for="box1"></label> 
<input id="box2" type="checkbox" class="checkbox" value="bx2=1"> <label for="box1"></label> 

<script> 
    $(".checkbox").on("change", function() { 
     var values = []; 
     $('.checkbox:checked').each(function() { 
      var result = $(this).val(); 
      values.push(result); 
     }); 
     var key = $(".link").html(values.join("&")); 
     document.write('<a href="http://www.example.com/test.html?' + key + '">Open here</a>'); 
    }); 
</script> 

預期的結果是「http://www.example.com/test.html?bx1=1&bx2=1」。 我希望有人能幫助我。

感謝這些行:

<input id="box1" type="checkbox" class="checkbox" value="bx1=1"> <label for="box1"></label> 
<input id="box2" type="checkbox" class="checkbox" value="bx2=1"> <label for="box1"></label> 
<a class="link" href="http://www.example.com/test.html">Open here</a> 

<script> 
    $(".checkbox").on("change", function() { 
     console.log('fzef'); 
     var values = []; 
     $('.checkbox:checked').each(function() { 
      var result = $(this).val(); 
      values.push(result); 
     }); 
     $(".link").attr('href', 'http://www.example.com/test.html?' + values.join("&")); 
    }); 

    // Init link on page load 
    $(".checkbox").trigger("change"); 
</script> 

我得到我的價值觀之間的&on&,並沒有對IE瀏覽器。任何想法?謝謝!

+2

document.write()的?但是,您的預期結果是什麼?你有什麼錯誤? –

+0

對不起,我忘了把url放在這裏... – vloryan

+0

這是因爲你將'values'設置爲一個數組,所以「粘貼」的值基本上是一個對象,你需要從該對象中提取值... – odedta

回答

1

你可以這樣做:

<input id="box1" type="checkbox" class="checkbox" value="bx1=1"> <label for="box1"></label> 
<input id="box2" type="checkbox" class="checkbox" value="bx2=1"> <label for="box1"></label> 
<a class="link" href="http://www.example.com/test.html">Open here</a> 

<script> 
    $(".checkbox").on("change", function() {console.log('fzef'); 
     var values = []; 
     $('.checkbox:checked').each(function() { 
      var result = $(this).val(); 
      values.push(result); 
     }); 
     $(".link").attr('href', 'http://www.example.com/test.html?' + values.join("&")); 
    }); 

    // Init link on page load 
    $(".checkbox").trigger("change"); 
</script> 
+0

非常感謝您對此和所有其他建議!這很好用! – vloryan

+0

當我在瀏覽器中點擊** BACK **按鈕時,選中的複選框仍被選中,但鏈接不包含值。任何機會/想法更新此案件的鏈接? – vloryan

+0

查看我的編輯,您只需在頁面加載時強制更改事件。 – sylvain1264

-1

嘗試這樣的: 你需要document.write()

document.write('<a href="http://www.example.com/test.html?' + values.join('&') + '">http://www.example.com/test.html?' + values.join('&')+'</a>'); 

OR

$("button").on("click", function() { 
 
     var values = []; 
 
     $('.checkbox:checked').each(function() { 
 
      var result = $(this).val(); 
 
      values.push(result); 
 
     }); 
 
    
 
    
 
    
 
    $('#url').attr('href','http://www.example.com/test.html?' + values.join('&')); 
 
    $('#url').html('http://www.example.com/test.html?' + values.join('&')) 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input id="box1" type="checkbox" class="checkbox" value="bx1=1"> <label for="box1">first</label> 
 
<input id="box2" type="checkbox" class="checkbox" value="bx2=1"> <label for="box1">second</label> 
 
<button >Go</button> 
 
<a id="url"></a>

+0

'document.write()'將關閉現有文檔並開始在第一個複選框中單擊一個新文檔,然後您將永遠不能單擊另一個文檔或取消選中第一個文檔。 –

+0

但是OP沒有提及使用相同的頁面。他還用'document.write()'寫入 – prasanth

+0

OP使用'document.write()'是錯誤的。這只是他建立字符串的嘗試,但除非你有意要寫一個新文檔,否則你不能使用它,這顯然不是這裏的意圖。 –

0

你需要做的修正是在該行
var key = $(".link").html(values.join("&"));

您需要將其更改爲
var key = values.join("&");

爲什麼?
因爲代碼中沒有.link選擇器。

1

裹在表單中的複選框,給他們的名字,讓jQuery的做的工作與.serialize()

$(function() { 
 
    $("form").on("change", function() { 
 
    var href = "http://www.example.com/test.html", 
 
     params = $(this).serialize(); 
 

 
    if (params.length > 0) { 
 
     href += "?" + params; 
 
    } 
 

 
    console.log(href); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
    <input id="box1" name="bx1" type="checkbox" class="checkbox" value="1" />bx1 
 
    <input id="box2" name="bx2" type="checkbox" class="checkbox" value="1" />bx2 
 
</form>