jQuery如何获取复选框选中项后的文字?

JavaScript025

jQuery如何获取复选框选中项后的文字?,第1张

1、创建如下结构的测试文件-- Content,-- jquery-1.11.3.min.js,-- JquerySelect.html。

2、【获取】下拉框【选中值】:使用【.val()】。

3、【获取】下拉框【选中文本】:使用【.find("option:selected").text()】。

4、【获取】下拉框选中项的【索引】:使用【.get(0).selectedIndex】。

5、通过【value】值【设置】下拉框的选中项:使用【.val("value值")】。

6、通过【text】值【设置】下拉框的选中项。

7、通过【value】值【删除】下拉框的一个选项。

8、通过【text】值【删除】下拉框的一个选项。

//获取选中的文字

function getSelectText(editor) {

    if (!editor) return editor.focus()

    if (editor.document && editor.document.selection)

        return editor.document.selection.createRange().text 

    else if ("selectionStart" in editor)

        return editor.value.substring(editor.selectionStart, editor.selectionEnd) 

}

//调用方式

var text= getSelectText(document.getElementById("txtName"))

给我100分

我给你把代码贴上来

这个实现还是很容易的!

算了,你还是看着给把!

document.onmouseup=function(e){

content=window.getSelection().toString()

if(content!=''){

var len=content.length

var target=e.target

var position=target.innerHTML.indexOf(content)

var position2=position+len

var tempstr1=target.innerHTML.substring(0,position)

var tempstr2=target.innerHTML.substring(position2)

content="<span style='color:red'>"+content+"</span>"

target.innerHTML=tempstr1+content+tempstr2

}}

实现了选中了就变红,自己扩展下把!

还有这个暂时只在chrome下能用,ie 你自己改把!