连接查询代码与许多不同的div [英] Connecting query code with many different divs

查看:73
本文介绍了连接查询代码与许多不同的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有5个div

<div id="" class=""> Div11 </div>
<div id="" class=""> Div12 </div>
<div id="" class=""> Div13 </div>
<div id="" class=""> Div14 </div>
<div id="" class=""> Div15 </div>

和jquery代码类似:

And jquery code for like:

点击一些div来隐藏它。

On click some div to hide it.

现在当我在jquery $(DIV)中说。点击(function(){$( DIV)。hide();});

Now when I say in jquery $("DIV").click(function(){ $("DIV").hide(); });

我把它命名为DIV所以我可以问你这些问题:

I named it DIV so I can ask you these questions:


  1. ID或CLASS是否与其他所有人相同而另一个不同。

  2. 如果ID是不同的,如果我有第一个div id =div1和第二个 id =div2,直到最后,我怎么能将查询连接到每一个?

  1. Is ID or CLASS one that is going to be same as all others and the other one different.
  2. If ID is different and if i have first div id="div1" and second id="div2" and till last, how can I connect query to every single one of them?

我试过但它只能在一个div上工作...喜欢更好的问题:

I tried but it is working only on one div... Like even better question:

如果我在页面加载时将一些textarea设置为隐藏在jquery中。在div上点击它需要显示...我的脚本只能在其他人的1 div上工作,并且textarea不是应该隐藏的。

If I set some textarea as hide in jquery on page load. And on div click it need to show up... My script works only on 1 div on others not, and the textarea is not hidden as it should be.

更新:

我的真实情况是我有php文件和mysql数据库

My real thing is that I have php file and mysql database

mysql数据库包含名字和姓氏和姓氏
它有10个名字

mysql database contains first name and id and last name it have 10 names

在我的php文件中我有:

in my php file I have:

$sql = "SELECT * FROM names";
$result = $con->query($sql);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
    $fname = $row['firstname'];
    $id = $row['id'];
            echo $fname."

            <textarea id='div".$id."' class='divsss'>Enter last name:</textarea>

";

    }

}

然后我有jquery代码:

Then I have jquery code:

<script type="text/javascript">
        $(document).ready(function() {


                $('.divsss').keypress(function(event) {

                var key = (event.keyCode ? event.keyCode : event.which);
                if (key == 13) {

                    var lname = $('.divsss').val();

                    var forid = '<?php echo $id; ?>';

                    $.ajax({
                        method: "POST",
                        url: "intodb.php",
                        data: {ln: lname, fi: forid},
                        success: function(status) {
                        alert("lname: " + lname + "for id: " + forid);
                            $('.divsss').val('');

                        }

                    });
                    };
                }); 

        });
    </script>

它不会工作但是当我将它更改为具有相同ID的ID时,它仅用于第一次输出它有值为0或1.但是当我尝试按类时它不起作用。
并且在intodb.php里面我只更新了DB ...

It wont work but when I change it to ids with same id it worrk only for first output it have value as 0 or 1. But When I try by classes it is not working. And inside intodb.php I just have update into DB...

推荐答案

关于你的问题


ID或CLASS是否与其他所有人相同,而
与其他人不同。

Is ID or CLASS one that is going to be same as all others and the other one different.

id 属性按定义应该是唯一的,可用于标识DOM中的特定元素。 class 另一方面,属性可以共享我的多个元素,通常用于样式元素。

The id attribute by definition should be unique and can be used to identify a specific element within the DOM. The class attribute on the other hand can be shared my multiple elements and is generally used for styling the elements.


如果ID不同如果我有第一个div id =div1和第二个
id =div2,直到最后,如何将查询连接到每一个

If ID is different and if i have first div id="div1" and second id="div2" and till last, how can I connect query to every single one of them?

您可以通过多种方式完成此任务。 jQuery支持各种基于属性的选择器,因此您可以轻松地定位具有 id 的每个元素,如下所示:

You can accomplish this in a variety of ways. jQuery supports a wide variety of attribute-based selectors, so you could easily target every element that has an id that "starts-with div" as seen below :

// This would hide any elements that have an ID that starts with "div"
$('[id^="div"]').hide();

同样,您也可以将CSS类应用于所有这些元素以对它们进行分组并选择它们方式:

Likewise, you could also apply a CSS class to all of those elements to group them and select them that way :

<div class='your-class'></div>
<div class='your-class'></div>
<div class='your-class'></div>

以及:

// This would hide any elements that have the class "your-class"
$('.your-class').hide();

关于您的代码

关于您的实际代码,您需要确保在 keypress 事件中正确确定您要执行的操作:

With regards to your actual code, you'll need to ensure that you are properly scoping what you are trying to do within your keypress events :

// When a key is pressed within this element (presumably a <textarea> or <input>
$('.divsss').keypress(function(event) {
        // Store a reference to this for your AJAX callback
        var _self = $(this);
        // Get the key
        var key = (event.keyCode ? event.keyCode : event.which); 
        // If it was enter
        if (key == 13) {
            // Get the last name (of this element), using $(this)
            var lname = $(this).val();
            // Get an id predefined by your server-side PHP
            var forid = '<?php echo $id; ?>';
            // Make your AJAX post
            $.post('intodb.php',{ln: lname, fi: forid}, function(status){
                    alert("lname: " + lname + "for id: " + forid);
                    _self.val('');
            });
}); 

这篇关于连接查询代码与许多不同的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆