下面这段js代码不能在jquery1.9.1版本下运行,我用jquery1.8.3使用没问题。哪里出问题了?
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
jQuery(function($) {
$('.keywords-field a').click(function() {
var field = $(this).parent().prev('input[type=text]');
var keywords = $.map(field.attr('value').split(','), function(keyword) {
return $.trim(keyword);
});
var keywords = $.grep(keywords, function(keyword) {
return keyword.length > 0;
});
var op, keyword = $(this).text().substr(1);
if ($.inArray(keyword, keywords) >= 0) {
keywords = $.grep(keywords, function(keep) {
return keep != keyword;
});
op = '+';
} else {
keywords[keywords.length] = keyword;
op = '-';
}
field.attr('value', keywords.join(', '))
$(this).text(op + keyword);
return false;
});
});
</script>
<p class="field-keywords">
<p>
<label for="id_keywords_0">Keywords:</label>
<input id="id_keywords_0" name="keywords_0" type="hidden" /><input class="vTextField" id="id_keywords_1" name="keywords_1" type="text" class="form-control" /><p class='keywords-field'><a href='#'>+关键词1</a><a href='#'>+关键词2</a><a href='#'>+关键词3</a>'></p>
</p>
</p>
ringa_lee2017-04-10 17:57:16
field.attr('value')在1.9.1下返回undefined,在1.8.3下返回""
可能两个版本对attr的支持不一样, 用val()来获取input输入框的值吧