来自php字符串的动态链接

时间:2015-01-13 07:38:50

标签: javascript php jquery

我一直在用javascript来讨论如何做到这一点。我尝试的是根据从下拉菜单中选择的内容来更改链接。我试图在这里找到符合我需求的文章是不成功的。 这是我的代码:

<form action="" method="post" id="storefrm">
<?php
require ("dbconnect.php");
$db = new mysqli($servername, $username, $password, $db);
?>
<script src="https://code.jquery.com/jquery-2.1.3.js"></script>
<div class="label">Select Store:</div>

<select name="stores" id="stores">
<option value="">---Select---</option>
<?php
$queryusers = "SELECT * FROM `storeconst` ORDER BY `Store` ASC";
$db = mysqli_query($db, $queryusers);
while ( $d=mysqli_fetch_assoc($db)) {
echo "<option value='".$d['GenManageEmail']."'>".$d['Store']."</option>";
}
?>
</select>
</form>  
<script>
$('#sendto').click(function(){
$('#storefrm').attr('action', 'mailto:'+$('#stores').val());
$('#storefrm').submit();
});
</script>
<button name="send" id="sendto" type="button">Send</button>

我是一个php新手和javascript新手。如何在选择商店时更改链接或按钮。 感谢

2 个答案:

答案 0 :(得分:0)

<script>
    $('#select_box_id').change(function(){
          var newLink = $(this).val();
         $("#button_id").val("mailto:"+newLink);  //button selector 
    });
</script> 

检查一下.. http://jsfiddle.net/g6b5ywLh/2

答案 1 :(得分:0)

请参阅以下内容,

<div class="label">Select Store:</div>

<select name="stores" id="stores">
<option value="">---Select---</option>
<?php
$queryusers = "SELECT * FROM `storeconst` ORDER BY `Store` ASC";
$db = mysqli_query($db, $queryusers);
while ( $d=mysqli_fetch_assoc($db)) {
    echo "<option value='".$d['GenManageEmail']."'>".$d['Store']."</option>";
}
?>
</select> 
<script>
$('#stores').change(function(){
      var selval = $(this).val();
     $("#sendto").val("mailto:"+selval);
});
</script>

<button name="send" value="" id="sendto" type="submit">Send</button>

您需要添加jquery才能实现此功能

<强>编辑:

<form action="" method="post" id="storefrm">
<div class="label">Select Store:</div>
<select name="stores" id="stores">
<option value="">---Select---</option>
<?php
$queryusers = "SELECT * FROM `storeconst` ORDER BY `Store` ASC";
$db = mysqli_query($db, $queryusers);
while ( $d=mysqli_fetch_assoc($db)) {
    echo "<option value='".$d['GenManageEmail']."'>".$d['Store']."</option>";
}
?>
</select>
<button name="send" onclick="submfrm()" type="button">Send</button>
</form>
<script>
function submfrm(){
  $('#storefrm').attr('action', 'mailto:'+$('#stores').val());
  $('#storefrm').submit();
}
</script>