简体   繁体   English

如何使用ajax jQuery将div值传递给页面?

[英]How to pass div value to page using ajax jQuery?

I have one div where timer is set. 我有一个div设置了计时器。 On submit of page I want div value to insert into database 在提交页面时,我希望将div值插入数据库

I am trying to post it using ajax.Here is my code In this , instead of name I want to send div value. 我正在尝试使用ajax发布它。这是我的代码,而不是我想发送div值的名称。

I tried to get value using getElementById but not getting how to pass it. 我试图使用getElementById来获取价值,但没有获得如何传递价值的方法。 or else there any other way to get div value and store it in database. 否则还有其他方法来获取div值并将其存储在数据库中。

here is my code: 这是我的代码:

<div id="timecounter" >00:05:00</div>

<script>    
    $("button").click(function() {

    $.post("test_post.php", {
            name: "Donald Duck"
        },
        function(data, status) {
            alert("Data: " + data + "\nStatus: " + status);
        });
    });
 </script>

You can use innerHtml property as :- 您可以将innerHtml属性用作:-

divObj= document.getElementById("timecounter")

   alert (divObj.innerHTML); 

Refer link click here for all possible scenarios ... 请参阅链接单击此处以获取所有可能的情况...

First, find the element. 首先,找到元素。 The fastest way is by ID . 最快的方法是通过ID Next, use innerHTML to get the HTML content of the element. 接下来,使用innerHTML获取元素的HTML内容。

document.getElementById('timecounter').innerHTML;

Since you mentioned jQuery in both your question tag and title, here's a jQuery-specific solution to your problem: 由于您在问题标签和标题中都提到了jQuery ,因此以下是针对您的问题的jQuery特定解决方案:

var timecounter_val = $('#timecounter').html(); // Store HTML of `timecounter` `div` in `timecounter_val` variable.

If you want, you can also use javascript to access HTML of the div element. 如果需要,还可以使用javascript访问div元素的HTML。 Here's how you can do that. 这是您可以执行的操作。

var timecounter_val = document.getElementById('timecounter').innerHTML;

you can use $.ajax to do the action. 您可以使用$ .ajax进行操作。 Details on $.ajax or you can use the Details on $.post which you are currently using, just change the code as below $ .ajax的详细信息,或者您可以使用当前使用的$ .post详细信息 ,只需更改以下代码

$.post("test_post.php", {
        param: $("timecounter").html()
    },
    function(data, status) {
        alert("Data: " + data + "\nStatus: " + status);
    });

In server you can access it using the word param 在服务器中,您可以使用单词param来访问它

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM