简体   繁体   English

从表单元格获取编辑的单元格值

[英]Get edited cell value from table cell

I am displaying a table using php on an html page. 我在html页面上使用php显示表格。 I want to edit the cell contents (basically make changes to a value, and make ajax call to update the database). 我想编辑单元格的内容(基本上是更改一个值,并进行ajax调用来更新数据库)。 The requirement for me is to use an onblur function (I can get around with using onkeypress as well). 对我的要求是使用onblur函数(我也可以使用onkeypress来解决)。 Can anyone please tell me what would be the simplest way of getting the new value entered with the cell id? 谁能告诉我用单元格ID输入新值的最简单方法是什么?

The alert for x tells me its "object HTMLTableCellElement" x的警报告诉我它的“对象HTMLTableCellElement”

Thanks!!! 谢谢!!!

foreach($_RESPONSE['VENDOR_LIST'] as $r){
     echo  "<tr><td>".$r['fdEmail']."</td><td id='companyname_".$r['fdId']."' contenteditable='true' onblur='updateValue(id)';>".$r['fdCompanyName']." ".$r['fdId']."</td></tr>";
}

Javascript method Javascript方法

function updateValue(thisdata){
        alert(""+thisdata);
    var x=document.getElementById(""+thisdata);
    if(x){
        alert(x);
        //var r = x.value;
    }
    else
            alert("not found");

}

The "value" of a table cell is not actually considered a value by javascript, it's just HTML. javascript并不真正将表格单元格的“值”视为一个值,而只是HTML。 Try alerting x.innerText . 尝试提醒x.innerText See this question for obtaining cell contents by ID. 有关通过ID获取单元格内容的信息,请参见此问题

Also, I know everyone hates hearing this, but this would be much easier with jQuery . 另外,我知道每个人都讨厌听到此消息,但是使用jQuery会容易得多。

In your HTML, change updateValue(id) to updateValue(this.id) ; 在您的HTML中,将updateValue(id)更改为updateValue(this.id)

Then put this inside your first if statement in the updateValue function. 然后将其放在updateValue函数的第一个if语句中。

if(x.innerText){
    alert(x.innerText);
} else {
    // Firefox support
    alert(x.textContent);
}

In your code 在你的代码中

var x=document.getElementById(""+thisdata);

Here x is an object, you need to write the required function to change the values or to get the values. 这里x是一个对象,您需要编写所需的函数来更改值或获取值。

alert(x.innerHTML);

will give you the value of the cell. 将为您提供单元的价值。

Id mentioned in HTML td tag and the id in your getElementById deos not match. HTML td标签中提到的ID与您的getElementById deos中的ID不匹配。 (Use error console in your browser to get the javascript errors) (使用浏览器中的错误控制台获取javascript错误)

You may have to change your function like this 您可能需要像这样更改功能

function updateValue(thisdata){
    var x=document.getElementById("companyname_"+thisdata);
    if(x){
        alert(x.innerHTML);
    }
    else
            alert("not found");

}

Your HTML code to : 您的HTML代码为:

foreach($_RESPONSE['VENDOR_LIST'] as $r){
     echo  "<tr><td>".$r['fdEmail']."</td>
                <td id='companyname_".$r['fdId']."' contenteditable='true' onblur="updateValue('companyname_".$r['fdId']."');">".$r['fdCompanyName']." ".$r['fdId']."</td></tr>";
}

NOTE : I have not tested the code please make changes accordingly, Post here if you find it difficult :) 注意:我尚未测试代码,请进行相应的更改,如果发现困难,请在此处发布:)

ALWAYS USE jQuery, It makes your task simple :) 总是使用jQuery,它使您的任务变得简单:)

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

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