如何在更改下拉列表时更改href url?

时间:2017-07-08 01:23:40

标签: javascript php html

这是我的代码

<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 URL

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

要更新,以便该项目指向从下拉列表中选择的选项 我怎么能这样做?
任何帮助将不胜感激!

注意:我无法使用选项

中的值

2 个答案:

答案 0 :(得分:1)

这就是我想你想要的......

它会将所选选项的text作为&item=之后要添加的数据 请注意,我修改了一些HTML以使其正常工作 请参阅代码中的注释。

&#13;
&#13;
// 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);
}
&#13;
<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>
&#13;
&#13;
&#13;

我假设$id$price是PHP变量。

答案 1 :(得分:0)

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

这可以更改<a>

的网址