我有一个简单的jQuery声明:
$(document).ready(function() {
$('#ltc_link').click(function() {
$('#USERDEFINE1').val( 'LT' );
$('#PHONENIGHT').val( '(111)111-1111' );
$('#INTERNET').val( 'NONE' );
$('#last_purchase').val( 'N/A' );
$('#last_purchase_date').val( 'N/A' );
});
});
Run Code Online (Sandbox Code Playgroud)
#ltc_link单击链接时,它会填充输入字段.
如果已经输入一个或所有字段的文本,而不是我不希望点击/链接功能覆盖用户输入的内容.我知道在javascript中我可以这样做:
if (!third.value) { third.value = '(111)111-1111'; }
if (!fourth.value) { fourth.value = 'NONE'; }
if (!fifth.value) { fifth.value = 'N/A'; }
if (!sixth.value) { sixth.value = 'N/A'; }
Run Code Online (Sandbox Code Playgroud)
需要一些jQuery语法帮助.提前致谢.
我建议如下:
var defaults = {
USERDEFINE1 : 'LT',
PHONENIGHT : '(111)111-1111',
INTERNET : 'NONE',
last_purchase : 'N/A',
last_purchase_date : 'N/A'
};
$('#ltc_link').click(function() {
// selects the elements
$('#USERDEFINE1, #PHONENIGHT, #INTERNET, #last_purchase, #last_purchase_date').val(
function(i, v) {
// checks if the current value 'v' is an empty string,
// if it is supplies the default value (from the object), if not
// it sets the value to the current value
return v == '' ? defaults[this.id] : v;
});
});
Run Code Online (Sandbox Code Playgroud)
参考文献: