将多个变量传递给Javascript函数

时间:2014-10-08 02:12:12

标签: javascript php function variables

我能够将一个变量(ID)传递给javascript函数,但是当我去添加businessname变量时,popupdiv不会显示。

按钮显示和传递ID(这可行)

<input type='button' id='report-submit' value='Go' onclick='displayDiv(".$id.")'>

Javascript(有效)

function displayDiv(divid) {
    e=document.getElementById("zbadmin-"+divid);
    strUser=e.options[e.selectedIndex].value;
    if (strUser=='2') {
        document.getElementById('def').style.display = "block";
        document.getElementById('link-id').href="delete?id="+divid;
    }

}

但是当我添加businessname时,无法正常工作

<input type='button' id='report-submit' value='Go' onclick='displayDiv(".$id.",'".$businessname."')'>

Javascript(无效)

function displayDiv(divid,divbizname) {
    e=document.getElementById("zbadmin-"+divid);
    strUser=e.options[e.selectedIndex].value;
    if (strUser=='2') {
        document.getElementById('def').style.display = "block";
        document.getElementById('link-id').href="delete?id="+divid+"&businessname="+divbizname;
    }

}

我错过了一些非常简单的事情吗?我用纯文本替换它,它也没有用。


修改 继承按钮之前的代码。它从SQL表中获取数据并生成下拉列表。作为测试,我将$ id作为两个变量放在下面的代码中,它工作正常并显示弹出窗口。

while($row = mysql_fetch_array($proposal_result)) {
        $date=substr($row["date"], 0, 50);
        $formatted_date=date('d/m/Y', strtotime($date));
        $id=substr($row["idproposal"], 0, 50);
        $businessname=substr($row["businessname"], 0, 50);
        $status=substr($row["status"], 0, 50);
        $staff=substr($row["staff"], 0, 50);
        $file_access='<storage-bucket>';
        $file_name='proposal_'.$id.'_'.$businessname.'.pdf';
        $file=$file_access.$file_name;
        print "<tr><td>".$formatted_date."</<td><td>".$id."</td><td width='25px'><a href='".$file."'target='_blank'><img src='".$images."/attachment.png' alt='file'></a></td><td>".$businessname."</td><td>".$staff."</td><td>".$status."</td><td>
            <div>
                <select id='zbadmin-".$id."' name='zbadmin-".$id."' class='dropdowns'  required>
                    <option value='0'>Select and action...*</option>
                    <option value='1'>Change Status for ID#".$id."</option>
                    <option value='2'>Delete Proposal</option>
                </select>
           <input type='button' id='report-submit' value='Go' onclick='displayDiv(".$id.",".$id.")'></div>

如果我使用$ id,$ date它可以正常工作,但是只要我用字符串替换第二个变量,它就不会显示下面的popupdiv:

<div id='def'>
                    <div id='popup'>
                        <form name='deletefeedback' action='' method='post'>
                            <!--<img id='close' src='images/3.png'> CLOSE ICON-->
                            <h2>Reason for deleting proposal <script>function getID() { alert($divid);window.location='?divid='+$divid';}</script></h2>
                            <hr>
                            <input id='deletereason' type='radio' name='deletereason' class='radio' value='Added by mistake'>Added by mistake<br />
                            <input id='deletereason' type='radio' name='deletereason' class='radio' value='Added by mistake'>No longer required<br />
                            <input id='deletereason' type='radio' name='deletereason' class='radio' value='Added by mistake'>Incorrect Information Provided<br />
                            <input id='deletereason' type='radio' name='deletereason' class='radio' value='Added by mistake'>Reason 4<br />
                            <textarea name='deletecomments' placeholder='Comments...'></textarea><br />
                            <a href='' id='link-id'><input type='button' id='report-submit' value='Delete Proposal'></a><a href='/zerobooks-admin-dashboard'><input type='button' id='report-submit' value='Cancel'></a>
                        </form>
                    </div>
                </div>

3 个答案:

答案 0 :(得分:2)

你在第二个参数之前添加一个奇怪的单引号组合,摆脱它们:

onclick='displayDiv(".$id.", ".$businessname.")'

答案 1 :(得分:0)

搞定了。在passing a parameter with onclick function is read as a variable when it should be read as a string

找到了答案

基本上,我只是添加了反斜杠来传递参数作为字符串

 <input type='button' id='report-submit' value='Go' onclick='displayDiv(".$id.",\"".$businessname."\")'>

答案 2 :(得分:0)

  

使用

    <input type='button' id='report-submit' value='Go' onClick="displayDiv('<?php echo $id ?>', '<?php echo $businessname ?>');">
  

实施例

<?php 
$id = '1'; 
$businessname = 'Business Name'; 
?> 
<html> 
    <head> 
    <script type="text/javascript"> 
    function displayDiv(id, businessname) { 
       alert(id);
       alert(businessname);
    } 
    </script> 
</head> 
    <body> 
        <input type='button' id='report-submit' value='Go' onClick="displayDiv('<?php echo $id ?>', '<?php echo $businessname ?>');">
    </body> 
</html>