2017-05-23 52 views
0

輸入值爲:發送輸入值ActionLink的

<input type="text" id="quantity" value="1" size="2"/> 

ActionLink的替換Url.Action:

<a href="@Url.Action("AddToCart", "Shop", new {[email protected]_id, quantity = "xxx"})" id="lnk">To cart</a> 

和JS那裏我試圖取代值:

<script> 
$("#lnk").click(function (evt) { 
    var fakeUri = $("#lnk").prop("href"); 
    var uri = fakeUri.replaceWith("xxx", $("#quantity").val()); 
    uri = $("#lnk").prop("href", uri); 
}); 
</script> 

我覺得問題在於JS,價值不是替代,客戶端發送「xxx」

+0

嘗試'ATTR( 「HREF」)',DOC這裏https://stackoverflow.com/questions/179713/how-to -change-the-href-for-a-hyperlink-using-jquery –

+0

@CarstenLøvboAndersen沒有工作 –

回答

0

這應該工作

一個你的href="@Url.Action("AddToCart"

它會返回一個href看起來像@Url.Action("addToCart",僅此而已的問題。監守的"

或者你也可以做到這一點<a href="@(Url.Action("AddToCart", "Shop ", new {[email protected]_id, quantity = "xxx "}))">

^添加()圍繞url

$("#lnk").click(function(event) { 
 
    event.preventDefault(); 
 
    var fakeUri = $("#lnk").attr("href"); 
 
    var uri = fakeUri.replace("xxx", $("#quantity").val()); 
 
    uri = $("#lnk").attr("href", uri); 
 
    window.location = uri; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" id="quantity" value="1" size="2" /> 
 

 
<a href='@Url.Action("AddToCart", "Shop ", new {[email protected]_id, quantity = "xxx "})' id="lnk">To cart</a>

+0

是否有可能在沒有window.location的情況下做到這一點?因爲我有一些麻煩, –

0

讓我們嘗試做不同的方式, 首先,我們添加事件數量變化的監聽者,每次發生時,我們都會替換href屬性值,注意我們HOULD使用attrprop 像這樣

<a href="#" id="lnk">To cart</a> 

JS

$(document).on("change","#quantity",function() { 
    var qnty = $("#quantity").val(); 
    var theuri = "@Url.Action("AddToCart", "Shop")[email protected]_id&quantity="+qnty; 
$("#lnk").attr("href", theuri); 
}); 
+0

tryed你的方式,但沒有,href仍然等於# –

+0

我編輯,現在嘗試。 – Munzer

+0

不了了之,不知道爲什麼,所有參考文獻都包含在內 –