如何将突出显示的文本从textarea提交到数据库

时间:2019-07-12 11:19:13

标签: javascript jquery html css

点击事件时,我必须将文本区域中突出显示的文本保存到数据库中 我该怎么做。我从中找到了一些代码,但这对我不起作用。

$('#send').on("click", function(){
	ShowSelection();
});

function ShowSelection()
	{
		var textComponent = $('#my-content span').val();
		console.log(textComponent);
		var selectedText;

		if (textComponent.selectionStart !== undefined)
		{// Standards Compliant Version
			var startPos = textComponent.selectionStart;
			var endPos = textComponent.selectionEnd;
			selectedText = textComponent.value.substring(startPos, endPos);
		}
		else if (document.selection !== undefined)
		{// IE Version
			textComponent.focus();
			var sel = document.selection.createRange();
			selectedText = sel.text;
		}
		alert("You selected: " + selectedText);
	}

1 个答案:

答案 0 :(得分:0)

您的代码有很多错误:

borderOnForegroundselectionStart是表单元素(输入,文本区域)的属性,但是selectionEnd显然在寻找$('#my-content span')元素。

<span>将返回一个字符串(在输入的情况下,该字符串是该输入的值,但在您的情况下,该字符串将为空,因为您将其应用于span元素。

$('#my-content span').val():由于textComponent.selectionStart是字符串而不是HTML元素,因此textComponent上没有属性selectionStart

-

textComponent元素(see here)的工作示例:

<textarea>

var startPos = $('textarea')[0].selectionStart // etc. 元素(see here)的工作示例:

<span>
相关问题