我正在尝试检查标签文本是否与文本框匹配,如果匹配,则将该特定标签文本设置为是,否则不,但在我的代码中我不确定出了什么问题,但这并没有发生,因为它显示“不” "它自己
HTML
<input class="master" value="1">
<label class="user_label" >1</label>
<label class="user_label" >0</label>
<label class="user_label" >1</label>
JS:
$(function() {
var master = $('input.master').get(0).value; // get the master value
var fn = function() {
return this.text === master ? "yes" : "noo";//if current text-box matches master,then yes else no
};
$('label.user_label').text(fn); // loop and replace text for each user input
});
最佳答案
this.text
将在 fn
中被 undefined
,因为 this
是一个 DOM 节点,它没有'有 text
属性。
您可以将其包装为 jQuery 对象并使用 text()
方法:
var fn = function() {
return $(this).text() === master ? "yes" : "noo";
}
关于javascript - 如果文本框值匹配,则将该特定标签文本设置为 yes else no,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32863154/