2017-07-08 61 views
1

這是我的代碼如何更改下拉菜單時更改href網址?

<tbody> 
<tr> 
    <td>$id</td> 
    <td> 
    <div class='form-group'> 
    <div class='col-sm-10'> 
     <select id='form-control' class='form-control' onchange='leaveChange()'> 
     <option value='$prices'>price</option> 
     <option value='$Sale'>Sale</option> 
     <option value='$payment'>payment</option> 
     </select>"; 
     ?>  
     <script> 
     function leaveChange() { 
     if (document.getElementById("form-control").value == ""){ 
      document.getElementById("enableordisable").innerHTML = "Enable"; 
      document.getElementById("status").innerHTML = "Disabled"; 
     }  
     if(document.getElementById("form-control").value == 1){ 
      document.getElementById("enableordisable").innerHTML = "Disable"; 
      document.getElementById("status").innerHTML = "Enabled"; 
     }   
     } 
     </script> 
    <?php echo "</div></td> 
    <td><p id='status'>---</p></td> 
    <td> <a id='enableordisable' type='addToTable' class='btn waves- effect waves-light btn-custom' href='function/enabledisable.php?stuff=$id&price=$price'>price</a></td> 

當從下拉列表中選擇一個值,我想要做的是,我想在按鈕

href='function/enabledisable.php?stuff=$id&price=$price&item' 

這href網址進行更新,這樣項目將指向從下拉列表中選擇的選項。
我該怎麼做?
任何幫助將不勝感激!

注:我無法使用值的選項

+0

通過下拉更改事件觸發href的值更改。創建事件並調用它來修改該值。 –

+0

我建議在你的下拉菜單中放置第一個選項,如<'option value ='0'>在這裏選擇......'如果你想在第一個選擇中選擇第一個選項時觸發你的功能。 – Rence

回答

1

這裏的網址是什麼,我想你想......

它將獲得所選選項的text作爲要在&item=之後添加的數據。
請注意,我修復了一點HTML以使其工作。
查看代碼中的評論。

// Pass the base href to JS 
 
var baseHref = "function/enabledisable.php?stuff=$id&price=$price"; 
 

 
// On change event function 
 
function leaveChange(el) { 
 
    if (document.getElementById("form-control").value == ""){ 
 
    document.getElementById("enableordisable").innerHTML = "Enable"; 
 
    document.getElementById("status").innerHTML = "Disabled"; 
 
    }  
 
    if(document.getElementById("form-control").value == 1){ 
 
    document.getElementById("enableordisable").innerHTML = "Disable"; 
 
    document.getElementById("status").innerHTML = "Enabled"; 
 
    } 
 

 
    // Set the additional GET data to the URL 
 
    var index = el.selectedIndex; 
 
    var newHref = baseHref+"&item="+el.getElementsByTagName("option")[index].text 
 
    console.log(newHref); 
 
}
<table> 
 
    <tbody> 
 
    <tr> 
 
     <td>$id</td> 
 
     <td> 
 
     <div class='form-group'> 
 
      <div class='col-sm-10'> 
 
      <select id='form-control' class='form-control' onchange='leaveChange(this)'> 
 
       <option value='$prices'>price</option> 
 
       <option value='$Sale'>Sale</option> 
 
       <option value='$payment'>payment</option> 
 
      </select> 
 
      </div> 
 
     </div> 
 
     </td> 
 
     <td><p id='status'>---</p></td> 
 
     <td> <a id='enableordisable' type='addToTable' class='btn waves- effect waves-light btn-custom' href='function/enabledisable.php?stuff=$id&price=$price'>price</a></td> 
 
    </tr> 
 
    </tbody> 
 
</table>

我認爲$id$price是PHP變量。

+1

非常感謝你的男人!這工作我不能感謝你,真的很感謝幫助:P –

0
document.getElementById("enableordisable").href = 'function/enabledisable.php?stuff=$id&price=$price&item'; 

這裏面可以改變的<a>