我有span
点击后可以input
进行实时更新。在输入blur
上我要检查的值是否与之前的值相同(因为我只想在值改变时更新)。如何使用此代码在jquery中实现此目的?
$("ul.pagination-list").on 'click', 'span[editable=text]', ->
input = $("<input />",
type: "text"
name: "editarea"
id: $(this).parent().attr("id")
value: $(this).html()
)
$(this).replaceWith input
$("ul.pagination-list").on 'blur', 'input[name=editarea]', ->
### here i want to check whether value is changed ###
span = $("<span />",
class: 'span3'
editable: 'text'
).text($(this).val())
$(this).replaceWith(span)
答案 0 :(得分:1)
您可以使用属性 value
包含初始值的事实,而属性 value
(由{读取) {1}} function)包含当前值。您可以使用val()
来读取初始值。
示例:
$(this).attr('value')
答案 1 :(得分:0)
您始终可以使用全局变量,然后检查是否有任何更改。这可能不是最好的解决方案,但它会起作用。
首先创建一个新的全局变量,默认值为""
var temp = "";
然后在blur
方法上,当它被调用时添加:
if(temp !== $(this).val()){
//Do something
}else{
//Do something
}
此外,请不要忘记在此之后为temp
分配新值
temp = $(this).val()