【问题标题】:javascript to reference input IDs in php loop and pass values back to same input IDjavascript在php循环中引用输入ID并将值传递回相同的输入ID
【发布时间】:2012-11-07 05:14:07
【问题描述】:

我有一个表单,它本质上是一个自动完成输入框。我可以让它完美地适用于单个文本框。我需要做的是使用 php for 循环创建唯一的输入框。这将使用计数器 $i 为每个输入框赋予唯一的名称和 ID。

问题是输入框的唯一值需要传递给javascript。这会将输入的数据传递给外部 php 页面并返回 mysql 结果。

如前所述,我在单个输入框上工作,但需要帮助将正确的值传递给 javascript 并将其返回到正确的输入框。

工作解决方案的现有代码(仅第一行有效,所有其他行更新第一行输入框)

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    function lookup(inputString) {
        if(inputString.length == 0) {
            // Hide the suggestion box.
            $('#suggestions').hide();
        } else {
            $.post("autocompleteperson.php", {queryString: ""+inputString+""}, function(data){
                if(data.length >0) {
                    $('#suggestions').show();
                    $('#autoSuggestionsList').html(data);
                }
            });
        }
    } // lookup

    function fill(thisValue) {
        $('#inputString').val(thisValue);
        setTimeout("$('#suggestions').hide();", 200);
    }
</script>
</head>
<body>
<form name="form1" id="form1" action="addepartment.php" method="post">
<table>
<?  for ( $i = 1; $i <=10; $i++ ) { ?>
    <tr>
        <td> <? echo  $i; ?></td>
        <td>
            <div>
                <input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" />
            </div>
            <div class="suggestionsBox" id="suggestions" style="display: none;">
                <img src="upArrow.png" style="position: relative; top: -12px; left: 20px;" alt="upArrow" />
            <div class="suggestionList" id="autoSuggestionsList">
                &nbsp;
            </div>
            </div>  
        </td>
    </tr>
<?  }   ?>
</table>
</body>
</html>

autocompleteperson.php 的代码是:

$query = $db->query("SELECT fullname FROM Persons WHERE fullname LIKE '$queryString%' LIMIT 10");
if($query) {
    while ($result = $query ->fetch_object()) {
        echo '<li onClick="fill(\''.$result->fullname.'\');">'.$result->fullname.'</li>';
    }
} 

为了让它适用于所有行,我在每个文件名中都包含了计数器 $i,如下所示:

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    function lookup(inputString<? echo  $i; ?>) {
        if(inputString<? echo  $i; ?>.length == 0) {
            // Hide the suggestion box.
            $('#suggestions<? echo  $i; ?>').hide();
        } else {
            $.post("autocompleteperson.php", {queryString: ""+inputString<? echo  $i; ?>+""}, function(data){
                if(data.length >0) {
                    $('#suggestions<? echo  $i; ?>').show();
                    $('#autoSuggestionsList<? echo  $i; ?>').html(data);
                }
            });
        }
    } // lookup

    function fill(thisValue) {
        $('#inputString<? echo  $i; ?>').val(thisValue);
        setTimeout("$('#suggestions<? echo  $i; ?>').hide();", 200);
    }
</script>
</head>
<body>
<form name="form1" id="form1" action="addepartment.php" method="post">
<table>
<?  for ( $i = 1; $i <=10; $i++ ) { ?>
    <tr>
        <td> <? echo  $i; ?></td>
        <td>
            <div>
                <input type="text" size="30" value="" id="inputString<? echo  $i; ?>" onkeyup="lookup(this.value);" onblur="fill();" />
            </div>
            <div class="suggestionsBox" id="suggestions<? echo  $i; ?>" style="display: none;">
                <img src="upArrow.png" style="position: relative; top: -12px; left: 20px;" alt="upArrow" />
            <div class="suggestionList" id="autoSuggestionsList<? echo  $i; ?>">
                &nbsp;
            </div>
            </div>  
        </td>
    </tr>
<?  }   ?>
</table>
</body>
</html>

The autocomplete suggestion works (although always shown on first row) but when selecting the data always returns to row 1, even if input was on row 5. I have a feeling this has to do with the fill() but not sure ?是因为自动完成页面代码吗?或者引用ThisValue的填充是否与它有关。

此页面的示例(如上)可以找到here

感谢您的帮助,非常感谢。

更新 - LOLO

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    function lookup(i, inputString) {
        if(inputValue.length == 0) {
            // Hide the suggestion box.
            $('#suggestions' + i).hide();
        } else {
            $.post("autocompleteperson.php", {queryString: ""+inputString+""}, function(data){
                if(data.length >0) {
                    $('#suggestions' + i).show();
                    $('#autoSuggestionsList' + i).html(data);
                }
            });
        }
    } // lookup

    function fill(i, thisValue) {
        $('#inputString' + i).val(thisValue);
        setTimeout("$('#suggestions' + i).hide();", 200);
    }
</script>
</head>
<body>
<form name="form1" id="form1" action="addepartment.php" method="post">
<table>
<?  for ( $i = 1; $i <=10; $i++ ) { ?>
    <tr>
        <td> <? echo  $i; ?></td>
        <td>
            <div>
                <input type="text" size="30" value="" id="inputString<?php echo $i; ?>" onkeyup="lookup(this.value);" onblur="fill();" />
            </div>
            <div class="suggestionsBox" id="suggestions<? echo  $i; ?>" style="display: none;">
                <img src="upArrow.png" style="position: relative; top: -12px; left: 20px;" alt="upArrow" />
            <div class="suggestionList" id="autoSuggestionsList<? echo  $i; ?>">
                &nbsp;
            </div>
            </div>  
        </td>
    </tr>
<?  }   ?>
</table>
</body>
</html>

谷歌浏览器发现以下错误:

第一行输入,第二行失去焦点

【问题讨论】:

    标签: php javascript mysql loops


    【解决方案1】:

    您不能将 JS 与 PHP 混合使用。 PHP 是一种服务器端语言,你可以用它来渲染动态 JS,但是在它渲染之后你就不能在 JS 中使用 PHP。首先你应该改变你的JS函数,我建议添加像i这样的参数,其中将包含输入数字:

    <script type="text/javascript">
        function lookup(i, inputString) {
            if(inputString.length == 0) {
                // Hide the suggestion box.
                $('#suggestions' + i).hide();
            } else {
                $.post("autocompleteperson.php", {queryString: ""+inputString+""}, function(data){
                    if(data.length >0) {
                        $('#suggestions' + i).show();
                        $('#autoSuggestionsList' + i).html(data);
                    }
                });
            }
        } // lookup
    
        function fill(i, thisValue) {
            $('#inputString' + i).val(thisValue);
            setTimeout("$('#suggestions" + i + "').hide();", 200);
        }
    </script>
    

    然后更改此函数的调用并提供编号:

    <input type="text" size="30" value="" id="inputString<? echo  $i; ?>" onkeyup="lookup(<? echo $i; ?>, this.value);" onblur="fill(<? echo $i; ?>);" />
    

    应该够了。

    【讨论】:

    • 感谢@Lolo,按照您的建议进行了更改。在问题底部提供了更新的代码。当用户提供输入时,现在不建议自动完成?我是否被正确传递给查找(我,输入字符串)?再次感谢。
    • 从您更新的问题中可以看出,您没有更改&lt;input&gt; 标记中的调用。尝试将事件处理程序更改为 onkeyup="lookup(&lt;? echo $i; ?&gt;, this.value);" onblur="fill(&lt;? echo $i; ?&gt;);"
    • 谢谢洛洛。我已经进行了更改,并再次使用我当前的代码更新了问题。更新输入框内容时还是没有显示任何数据?例如shedatabase.co.za/versionfour/configdept2.php
    • 嗨,Lolo,谷歌浏览器确实显示了一些错误,如问题底部所示:“Uncaught ReferenceError: inputValue is not defined”和“Uncaught ReferenceError: i is not defined”再次感谢。
    • 选择不起作用,因为您使用不正确的 onclick 事件处理程序从 php 发送数据。尝试从 php 中的
    • 元素中删除 onclick 处理程序并将其绑定到 autoSuggestionsList 或每个
    • 但在 JS 中而不是在 PHP 中。
    【解决方案2】:

    问题在于您的代码是所有文本框都具有相同的ID,而for循环中的每个DIV

    <input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" />
    

    它应该是独一无二的,就像这个编辑一样。

    <input type="text" size="30" value="" id="inputString<?php echo $i; ?>" onkeyup="lookup(this.value);" onblur="fill();" />
    

    【讨论】:

    • 感谢 Yogesh,您认为您错过了我更新代码的问题的底部。这对于循环中的每个输入框都有一个唯一的 id。我认为 fill() / onblur 函数需要做一些工作来指定使用返回值更新哪个输入框。再次感谢,
    • @Smudger 是的,您必须更改 fill() 函数的代码才能获得正确的文本框 ID。这就是我们所需要的。
    • 谢谢,有什么建议让我迷路了吗?
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签