2017-10-09 93 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) 
    } 
}