最も単純な場合:
// binds a click-handler to inputs whose `name` attribute is equal to 'Edit':
$('input[name="Edit"]').click(function(){
// when the input is clicked, it looks to the previous input element
// that has a `required` attribute, and sets its `readonly` property,
// the `r` is the current readonly state (true or false)
$(this).prev('input[required]').prop('readonly',function(i,r){
// returns the value as the opposite of what it currently is
// if readonly is false, then it returns true (and vice-versa)
return !r;
});
});
JSフィドルデモ。
そして、button
:のテキストを変更するために提供します。
$('input[name="Edit"]').click(function(){
$(this)
.val(function(i,v){
return v === 'Edit' ? 'Finished' : 'Edit';
})
.prev('input[required]')
.prop('readonly',function(i,r){
return !r;
});
});
JSフィドルデモ。