Javascript 获取p标记内的文本并存储在变量中
你们如何在p标记中获取文本并将其存储在VueJS中的变量中 因为现在,它只是在一个p标记中显示它,但我希望p标记中的文本存储在一个变量中,以供以后使用 下面是我的html代码Javascript 获取p标记内的文本并存储在变量中,javascript,vue.js,vuejs2,Javascript,Vue.js,Vuejs2,你们如何在p标记中获取文本并将其存储在VueJS中的变量中 因为现在,它只是在一个p标记中显示它,但我希望p标记中的文本存储在一个变量中,以供以后使用 下面是我的html代码 {{result.LONG_D} 值:{{selected.id} 搜寻 这是我的JS代码 导出默认值{ 数据(){ 返回{ 结果:{}, 搜索:“, msg:null, 选定:“” }; }, 方法:{ runAPI:函数(疾病){ axios .get(“http://localhost:9000/api/di
{{result.LONG_D}
值:{{selected.id}
搜寻
这是我的JS代码
导出默认值{
数据(){
返回{
结果:{},
搜索:“,
msg:null,
选定:“”
};
},
方法:{
runAPI:函数(疾病){
axios
.get(“http://localhost:9000/api/disease/“+疾病)
.然后(response=>(this.results=response.data))
.catch(错误=>console.log(错误));
console.log(this.results);
},
过滤人员(){
if(this.searchQuery){
返回此.results.filter(项=>{
返回项.LONG\u D.startsWith(this.searchQuery);
});
}否则{
返回此结果;
}
}
}
一种方法是将一个函数放在接受值的标记中(当然它也可以返回值,这样显示就不会改变)。然后该函数可以将值存储在数据或局部变量中,您就可以得到它。
像这样:
{{}
中的任何内容都是JS,因此您可以使用函数,甚至可以使用逻辑(尽管不建议在模板中放入太多逻辑)。您可以这样做。
JS代码
export default {
data() {
return {
results: {},
search: "",
msg: null,
selected: ""
};
},
methods: {
runAPI: function(disease) {
axios
.get("http://localhost:9000/api/disease/" + disease)
.then(response => (this.results = this.filteredPeople()))
.catch(error => console.log(error));
console.log(this.results);
},
filteredPeople() {
if (this.searchQuery) {
return this.results.filter(item => {
return item.LONG_D.startsWith(this.searchQuery);
});
} else {
return this.results;
}
<select v-model="selected">
<option
v-for="result in results"
:key="result.LONG_D"
:value="{{ id: result.LONG_D, text: result.ICD_C }}">{{ result.LONG_D }}</option>
</select>
HTML代码
export default {
data() {
return {
results: {},
search: "",
msg: null,
selected: ""
};
},
methods: {
runAPI: function(disease) {
axios
.get("http://localhost:9000/api/disease/" + disease)
.then(response => (this.results = this.filteredPeople()))
.catch(error => console.log(error));
console.log(this.results);
},
filteredPeople() {
if (this.searchQuery) {
return this.results.filter(item => {
return item.LONG_D.startsWith(this.searchQuery);
});
} else {
return this.results;
}
<select v-model="selected">
<option
v-for="result in results"
:key="result.LONG_D"
:value="{{ id: result.LONG_D, text: result.ICD_C }}">{{ result.LONG_D }}</option>
</select>
{{result.LONG_D}
它会将LONG\u D的值存储在变量中吗?应该是的,然后也检查它。除了像Michael's Answer这样的函数外,如何将result.LONG\u D存储在变量中更改js代码并检查结果。@learn2code