2017-10-09 125 views
2

我正在尋找動態更改價格和id的值,並在後續錨鏈接中選擇不同的下拉菜單。比如如果我選擇3天,則價格將變爲24,並使用新的oID更改爲錨點和ID。在錨鏈接中動態更改值

注意:隱藏字段用於在數據庫中提交數據。 $_post['deadline']。是的,糾正網址。輸入錯誤。

<a class="btn btn-warning" href="https://example.com?price=38&id=DS123">order</a> 

HTML

<div class="col-sm-6"> 
<label for="price" class="control-lable">Urgency*:</label> 
<select class="form-control" name="price" id="price" onchange="func()" required> 
    <option value="">Select urgency</option> 
    <option value="24">3 Days</option> 
    <option value="38">5 Days</option> 
    <option value="62">7 Days</option> 
</select> 
</div> 

<input type="hidden" name="deadline" id="deadline" value="" /> 
<input type="hidden" name="oid" id="oid" value="" /> 

的Javascript

function func() { 
var option = $("#price").val(); 
var dLine = $('#deadline').val(); 
var mID = new Date().getTime(); 
var oID = 'Q'+Math.round(mID/1000) 

if (option =='24') { 
    dLine = 3 Days; 
    oID; 
} 

if (option =='38') { 
    dLine = 5 Days; 
    oID; 
} 

if (option =='62') { 
    dLine = 7 Days; 
    oID;  
    } 
}; 
+0

那麼如何處理隱藏字段呢? –

+0

我想你忘了包括?在.com https://example.com&price=38&id=DS123應更改爲https://example.com?price=38&id=DS123 –

回答

2

你有HTML不必要的隱藏字段,以及不必要的if條件的jQuery(因爲我無法看到和FRUITFULL使用那些在您的方案)

做它象下面這樣: -

function func() { 
 
    var option = $("#price").val(); 
 
    var mID = new Date().getTime(); 
 
    var oID = 'Q'+Math.round(mID/1000) 
 
    $('.btn-warning').attr('href',"https://example.com?price="+option+"&id="+oID) 
 
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="col-sm-6"> 
 
<label for="price" class="control-lable">Urgency*:</label> 
 
<select class="form-control" name="price" id="price" onchange="func()" required> 
 
    <option value="">Select urgency</option> 
 
    <option value="24">3 Days</option> 
 
    <option value="38">5 Days</option> 
 
    <option value="62">7 Days</option> 
 
</select> 
 
</div> 
 

 
<a class="btn btn-warning" href="https://example.com?price=38&id=DS123">order</a> 
 

 
<!-- not required <input type="hidden" name="deadline" id="deadline" value="" /> 
 
<input type="hidden" name="oid" id="oid" value="" />-->

注: -

網址: - https://example.com&price=38&id=DS123似乎-正確的,因爲它必須是https://example.com?price=38&id=DS123(檢查查詢字符串符號?有)

1

你可以把一個id像錨: <a id="btn-order" class="btn btn-warning" href="#">order</a> 然後在你的onchange功能(func),這樣做: $('#btn-order').attr('href', 'https://example.com?price=' + option + '&id=' + oID);

0

,而不是使用的onchange = FUNC(),你可以儘量保持它的清潔使用jQuery的。對():

$('#price').on('change', function(e){ 
     var $thisSelect = $(this), 
      $anchor = $('.btn.btn-warning'), 
      oID = 'Q'+Math.round((new Date().getTime())/1000), 
      newhref = 'https://example.com/?id='+oID+'&price='+$thisSelect.val(), 
      selectedText = $thisSelect.find('option:selected').text(); 

     $anchor.prop('href', newhref); 

     $('#deadline').val(selectedText); 
     $('#oid').val(oID); 
    }); 
0

香草js版:

function func() { 
    var e = document.getElementById("price"); 
    var price = e.options[e.selectedIndex].value; 
    var button = document.getElementsByClassName('btn btn-warning')[0]; 

    if (price && button) { 
     var mID = new Date().getTime(); 
     var oID = 'Q'+Math.round(mID/1000); 
     button.setAttribute('href', "https://example.com?price=" + price + "&id=" + oID) 
    } 
}