Javascript jqueryif语句检查是否有输入值,否则单击链接时自动填充
我有一个简单的Javascript jqueryif语句检查是否有输入值,否则单击链接时自动填充,javascript,jquery,Javascript,Jquery,我有一个简单的jQuery语句,如下所示: $(document).ready(function() { $('#ltc_link').click(function() { $('#USERDEFINE1').val( 'LT' ); $('#PHONENIGHT').val( '(111)111-1111' ); $('#INTERNET').val( 'NONE' ); $('#last_purchase').val(
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' );
});
});
单击链接#ltc_链接
时,它填充输入字段
如果已经有文本输入到一个或所有字段中,我不希望单击/链接功能覆盖用户输入的内容。我知道在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'; }
需要有关
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;
});
});
参考资料: