我试图在1个js文件中放入2个脚本,但我得到了:
SyntaxError: missing } after property list
note: { opened at line 9, column 19
但是就我检查所有大括号是否闭合而言,我不确定真正的问题是什么。
// country
jQuery( document ).ready( function( $ ) {
$('select[name="country"]').on('change', function() {
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
});
var CountryId = $(this).val();
if(CountryId) {
$.ajax({
url: '{{ url('getprovinces') }}/'+encodeURI(CountryId),
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="province"]').empty();
var options = data.map(function(state) {
return $("<option class='form-control'>").val(state.id)
.text(state.name);
});
$('select[name="province"]').empty().append(options);
}
});
}else{
$('select[name="province"]').empty().append("<option class='form-control' value='' selected>Select</option>");
}
});
});
错误来自此行:
url: '{{ url('getprovinces') }}/'+encodeURI(CountryId),
有什么主意吗?
答案 0 :(得分:1)
You need to escape the quotes或使用双引号引起来的字符串。否则,JS会认为字符串在url('
之后用引号引起来,然后在弹出变量名时感到困惑。
答案 1 :(得分:0)
尝试这样:
// country
jQuery( document ).ready( function( $ ) {
$('select[name="country"]').on('change', function() {
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
});
var CountryId = $(this).val();
if(CountryId) {
$.ajax({
url: '{{' + url('getprovinces') + '}}/' + encodeURI(CountryId),
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="province"]').empty();
var options = data.map(function(state) {
return $("<option class='form-control'>").val(state.id)
.text(state.name);
});
$('select[name="province"]').empty().append(options);
}
});
}else{
$('select[name="province"]').empty().append("<option class='form-control' value='' selected>Select</option>");
}
});
});
答案 2 :(得分:0)
尝试
url: url('getprovinces') + '/' + encodeURI(CountryId),